mysql SELECT where like and like

Viewed 188

I have a column called papers in a table called products that has values like this :

"1,2,3,4,5,6" and in other row it has like this "3,4,5"

I want to launch this query with php,

SELECT * from products where papers like "%5%" AND papers like "%3%" and papers like "%4%"

TL;DR

I want to select a column that has multiple strings, ignoring the order

1 Answers

In MySQL, you can use find_in_set():

select * 
from products 
where find_in_set('3', papers) and find_in_set('4', papers) and find_in_set('5', papers)

Note that your question suggests a flawed database design. You should not be storing multiple integer values in a single string column. Instead, you should have each integer value in a separate row, probably in another table, that would refer to the main table through a foreign key constraint. Recommended reading: Is storing a delimited list in a database column really that bad?.

Related