Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql check if numbers are in a comma separated list

Tags:

list

mysql

I have a table like this:

UID(int) NUMBERS(blob) ---------------------- 1        1,13,15,20 2        3,10,15,20 3        3,15 

And I would like to test if 3 and 15 are in the blob called NUMBERS. And can see the LIKE %% cannot be used

Only row with ID 2 and three scoulb be selected...

like image 962
Tillebeck Avatar asked Apr 20 '10 09:04

Tillebeck


People also ask

How do I find comma separated values in MySQL?

MySQL has an inbuilt function called FIND_IN_SET which will search for values within a comma separated values. It basically returns the index position of the first parameter within the second parameter. This can be alternatively used to replace the IN clause. WHERE FIND_IN_SET (item_description,'Mobile,laptop');

How do you find comma separated values in SQL query?

To check if value exists in a comma separated list, you can use FIND_IN_SET() function. Now you can insert some records in the table using insert command. Display all records from the table using select statement.

Can we store comma separated values in MySQL?

A better answer: Don't store a list of comma separated values. Store one value per row, and use a SELECT query with GROUP_CONCAT to generate the comma separated value when you access the database.


1 Answers

This one also works:

SELECT * FROM table WHERE 3 IN (NUMBERS) AND 15 IN (NUMBERS) 

using the IN will look into a comma separated string eg. these two

WHERE banana IN ('apple', 'banana', 'coconut') WHERE 3 IN (2,3,6,8,90) 

Info found on this page:

like image 99
Tillebeck Avatar answered Sep 18 '22 23:09

Tillebeck