select from where value in column with comma separated - mysql

Below is my case
1: have a table with values like
dataval
Doctor,Enginner
DOctor
Labour,Doctor
now i want to select all value which as doctor in it how can it be done , can it be done using some my sql built in function

You can use the function SELECT FIND_IN_SET() like this:
SELECT *
FROM YourTable
WHERE FIND_IN_SET('Enginners', YourFieldName) > 0;

Related

Select row where id in array of mysql row

So I have an database where I have a string in each row eg: 1,4,56,3,23
I want to find every row where say 1 is in it, so this row would be found a 1 is at the start but if I have 4,1,54,32,2 it wont find it.
This is the code I'm using:
WHERE ".$id." IN ( outcomes )
You can use LIKE because you have a string separated by comma.
SELECT * FROM users WHERE outcome LIKE '41,%' OR outcome LIKE '%,41,%' OR outcome LIKE '%,41';
The above code select users which have outcome 41 in the list.
Use FIND_IN_SET :
SELECT *
FROM table
where FIND_IN_SET(".$id.",outcomes);
You can use the INSTR function as well :
SELECT *
FROM table
WHERE INSTR(".$id.",'1') > 0

use function for each item in clause mysql

I want to use unhex function for each item in the string seperated by comma. In this string this binary id as string sent by front end.
select * from table where id in
('5534B753765411E6B3FA0021004B111C,1668309A6E6F11E6B405F819928F1AD1,4FE75C954EEB11E6BB970F782D79D6C8')
I want to use this like
select * from table where id in (unhex('5534B753765411E6B3FA0021004B111C'),unhex('1668309A6E6F11E6B405F819928F1AD1'),unhex('4FE75C954EEB11E6BB970F782D79D6C8'))
IN's also support SELECT statements, so you could try using that functionality like so:
SELECT *
FROM table WHERE id IN
(SELECT UNHEX('5534B753765411E6B3FA0021004B111C')
UNION ALL
UNHEX('1668309A6E6F11E6B405F819928F1AD1')
UNION ALL
UNHEX('4FE75C954EEB11E6BB970F782D79D6C8'));
select * from table where
FIND_IN_SET(id,'5534B753765411E6B3FA0021004B111C,
1668309A6E6F11E6B405F819928F1AD1,4FE75C954EEB11E6BB970F782D79D6C8');

How to query MySQL if data in one field look like 32;13;3;33 and the condition is where 3

I have a table in which one column is filled with data like 32;3;13;33;43
so
SELECT * FROM table;
gives something like
name ids
vegetables 13;3;63
fruits 37;73;333
When I'm querying MySQL like
SELECT * FROM table WHERE ids LIKE '%3%'
it gives me both records but obviously I want only this containing 3.
How to query MySQL correctly?
Try to use:
SELECT * FROM table WHERE CONCAT(';',ids,';') LIKE '%;3;%'
You will need to cover the case where it's the first in the list and the last.
SELECT * FROM table WHERE ids LIKE '%;3;%' OR LIKE '%;3' OR LIKE '3;%'
You can use FIND_IN_SET, if you replace the ; with a , before checking the value:0
SELECT *
FROM table
WHERE FIND_IN_SET('3', REPLACE(ids, ';', ','))

MYSQL explode search

I have a field that is build like this "1;2;3;4;8;9;11;"
If I want to search if a number is in this range I do it like this:
SELECT * FROM table WHERE [id] LIKE '[number];%' OR '%;[number];%'
Is there another more easy way where i can split the string?
Many thanks
If you are storing the values in a string, the best way to use like is as:
SELECT *
FROM table
WHERE concat(';', #numbers) like concat('%;', [id], ';%')
MySQL also offers find_in_set() when the delimiter is a comma:
SELECT *
FROM table
WHERE find_in_set(id, replace(#numers, ';', ',')
Use IN() with a comma delimited string of IDs
SELECT * FROM table WHERE id IN(1,2,3,4,8,9,11)
create another table called helper with 2 fields: number and id. you will need to programmatically create it by splitting the id field and add one row to this table for each number in each id.
create an index this table on number
select table.* from table where id in (select id from helper where number = 23)
you can use this
SELECT name ,`from`, find_in_set('4' , REPLACE(`from`, ';', ',')) as the_place_of_myval FROM reservation
having the_place_of_myval != 0
note: that in my query im searching for strings which have number 4
demo here in my demo there is two examples.

MySQL select using wildcard (but wildcard in field)

I have a mysql 5 table with a char field holding
DOG
DOUG
CAT
MOUSE
Now I want to SELECT on this field, finding any rows where that field exist within a string, like "DOGGY". (This is opposite of how you normally use a wildcard). So I want a select something like:
SELECT FROM TABLE WHERE FIELD IS SUBSTRING OF "DOGGY"
Is this possible?
select * from mytable where 'doggy' like concat('%',mycol,'%')
You should be able to use LOCATE() to achieve this:
SELECT * FROM MyTable WHERE LOCATE(MyField, 'DOGGY') != 0;