mysql SELECT WHERE string contains characters - mysql

I have a table field events which can contains:
Only a sequence of number, for example: 45
or a sequence of number divided by the symbol |, for example 55|65|76
I think i have to use LIKE, but i don't know how. Can you help me?
Thanks

I would recommend using CONCAT to add a pipe | before and after your field, and then using a LIKE search. Something like this:
SELECT *
FROM YourTable
WHERE concat('|',field,'|') like '%|45|%'
SQL FIddle Demo
However, I highly recommend trying to normalize your data. Considering storing these in separate rows which would make searching/maintaining much easier.

To fix your query, use:
Select * from tbl where events='45' or events like '%|45' or events like '%|45|%' or
events like '45|%'
but this is terribly slow and should not be used.
Instead, do as Marc B states and create a child table events (ID, event).

Related

Searching for entry with id in comma separated list in mysql

I want to get entries from a mysql table, which contain a given id within a comma separated list. I want to use regular expressions and the LIKE selector.
My current approach looks like this
SELECT * FROM table WHERE list LIKE '%,0,%';
with the problem being that this ignores the first and last element in a list like '0,1,2,3'.
I've tried using the | or operator to test for all possible cases.
SELECT * FROM table WHERE list LIKE '(%,0,%)|(^0,%)';
I've tried this with and without the ^ character and with and without the parenthesis, but in all cases this approach didn't even match the characters in the middle. In fact, the or operator doesn't seem to be working in even the simplest expressions like
SELECT * FROM table WHERE list LIKE '%(1|2)%';
You should fix your data model! DO not store lists of things -- especially numbers -- in a string. SQL has a great data model for storing lists: it is called a table.
If you are stuck with someone else's really, really, really bad choice of dta model, you can work around in. MySQL has a handy function, find_in_set(), that does what you want:
WHERE find_in_set('0', list) > 0
Concatenate commas to the start and the end of the list:
SELECT * FROM table WHERE concat(',', list, ',') LIKE '%,0,%';

How to do Base64 decode and Lower in single mysql query?

I am stucked in one of MySQl query. I want two process on one field. Let say the field is named 'title'. Now title has BASE64_encoded data in it. Now I want to fetch all the data in lower case with mysql. I tried below code. but I didnt get my result. Hope you help me.
SELECT LOWER(FROM_BASE64(title)) as title FROM my_table;
I know, both are working individually. but not together. Now How can I do both process in one mysql.
I found solution.
SELECT LOWER(CONVERT(FROM_BASE64(title) USING latin1)) as title FROM my_table;
You can try like this:
SELECT LOWER(X) as title FROM (select FROM_BASE64(title) as X from my_table);

Search comma separated string using LIKE in mysql

I have a table with 3 columns something like below,
expert table
id - 1589
name - Jhonny
expert_in - 1,12,8 (Values similar like this)
The experts_in contains another table's foreign key
experts_in table
id - 1
expert_in - painting
I want search experts who are expert in some jobs while searching for experts
SELECT * FROM `experts` WHERE expert_in LIKE 1%
The above query brings all experts with 11,12,13...etc. I want only exact word. I know LIKE will bring all. Is there any way to achieve this without altering table. Thanks in advance.
You should use REGEXP. Try this query:
SELECT * FROM experts
WHERE expert_in REGEXP '[[:<:]]1[[:>:]]';
Output: See Live Demo on SQLFiddle
Note: You can adjust searching string based on your requirement above REGEXP is searching exact word.
if you can alter the data (not the table/schema) you should append and prepend another comma, so you can search with where col like "%,123,%", this will always fit on an exact value. Otherwise you have to use regex with something like ,?123,?

Normalizing date in MySQL where one column contains comma delimited items

I've got a database with one field that contains a comma delimited list that I'd like to normalize. The field would look something like this...
16-BIT, 20 MHz, MICROPROCESSOR, PQFP100
It will not always have the same number of commas delimited items, however the only item I care about, and the one I want to pull out and normalize is always going to be the last one, in the case above 'PQFP100'.
I think I understand the sql to get the column out and into another table, but I don't know how to select only the PQFP100 part of it. Here's what I have so far...
insert ignore into part_pkg (pkg_name)
select part_desc
from part_raw
group by pkg_name;
I think I need something on the 'from part_raw' part but don't even know where to start :)
Hopefully this is clear enough. Thanks
if you select this:
select substring_index('16-BIT, 20 MHz, MICROPROCESSOR, PQFP100',',',-1)
you will get this:
PQFP100
So the insert statement must be something like this:
insert ignore into part_pkg (pkg_name)
select substring_index(part_desc',',',-1)
from part_raw
group by pkg_name;
Try this:
insert ignore into part_pkg (pkg_name)
select SUBSTRING_INDEX(part_desc, ',', -1)
from part_raw
group by pkg_name;

MySQL UNION query correct handling for 3 or more words

I've to ask your help to solve this problem.
My website has a search field, let's say user writes in "Korg X 50"
In my database in table "products" i have a filed "name" that holds "X50" and a field "brand" that hold "Korg". Is there a way to use the UNION option to get the correct record ?
And if the user enters "Korg X-50" ?
Thank you very much !
Matteo
May be you should use full-text search
SELECT brand, name, MATCH (brand,name) AGAINST ('Korg X 50') AS score
FROM products WHERE MATCH (brand,name) AGAINST ('Korg X 50')
As far as I understand you don't need UNION but something like
SELECT * FROM table1
WHERE CONCAT(field1, field2) LIKE '%your_string%'
On client side you get rid of all characters (like space, hyphen, etc) in your_string that appears in user input and cannot be in field1 or field2.
So, user input Korg X 50 as well as Korg X-50 becomes KorgX50.
you will need to get some form of searchable text.
either parse out the input for multiple key words and match each separately, or perhaps try to append them all together and match to the columns appended in the same way.
you will also need either a regex, or maybe a simpler search and replace to get rid of spaces and dashes after the append before the comparison.
in general, allowing users to search for open ended text strings is more complicated than 'what union do i use'... you will ideally also be worried about slight misspellings and capitalization, and keyword order.
you may consider pulling all keywords out from your normal record into a separate keyword list associated with each product, then use that list to perform your searches.
If you do not want to parse user input and use as it is, then you will need to use a query like this
select * from products where concat_ws(' ',brand,name) = user_input -- or
select * from products where concat_ws(' ',brand,name) like %user_input%
However, this query won't return result if user enters name "Korg X-50" and your table contains "Korg" and "X50", then you need to do some other thing to achive this. You may look at http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex however it won't be a complete solution. Look for text indexing libraries for that ex: lucene