0

I have one column having comma separated values:

id Column_name
== ==========
1  value1,value2,value3,value5
2  value2,value3,value4
3  value1,value3,value5
4  value1,value2,value4,value5

Now, I want a query that get all records having value2 OR value4

I need query like below if possible:

SELECT * FROM table_name WHERE FIND_IN_SET('value2,value4',`column_name`) 

So, it will return recode with id 1,2,4 (having value2 or value4)

I have form for user that send value1,value2 ..etc from check_box

How can I optimize my select Query?

sooper
  • 5,991
  • 6
  • 40
  • 65
  • You cannot optimize a query that uses string operations for searching lists. You need to structure your data properly, using a junction/association table. – Gordon Linoff May 13 '14 at 10:41

1 Answers1

3

Use it like this

SELECT * FROM table_name 
WHERE FIND_IN_SET('value2',column_name) > 0
or FIND_IN_SET('value4',column_name) > 0

But never, never, never store multiple values in one column!

You should actually rather change your DB structure to something like this

your_table
----------
id
name
other_columns


values table
------------
your_table_id
value
juergen d
  • 201,996
  • 37
  • 293
  • 362