use function for each item in clause mysql - 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');

Related

How to SELECT w/ LIKE and a '_' delimiter

I have a table with a name field that can have values like:
CHECK_5_20170909
CHECK_1_20170809
CHECK_11_20170809
CHECK_11_20170909
I would now like to query all fields that have a _1_ in the name, but ONLY them.
I tried this: SELECT * FROM tblName WHERE name LIKE '%_1_%';
but that shows me _11_ AND _1_ in my results.
When I try it with CHECKWHATEVER1WHATEVER20170909 and LIKE %WHATEVER1WHATEVER% it works, are there any special rules for _ in a MySQL Query?
Changing it to another delimiter in the MySQL DB would create a hell of work, is there any "workaround"?
You need to add a '\' before each underscore, otherwise its interpreted as a random "wildcard" character.
select * from
(
select 'CHECK_5_20170909' col
union
select 'CHECK_1_20170809'
union
select 'CHECK_11_20170809'
union
select 'CHECK_11_20170909'
) t
where col like '%\_1\_%'
try this using REGEXP
SELECT * FROM tblName WHERE name regexp '_1_';
it will return exact matches record from column for more reference read here

select from where value in column with comma separated

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;

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;