3

I have a MySQL table which contains comma-separated values like this:

first row=(3,56,78,12)  
second row=(6,44,2,3)  
third row=(67,4,2,7,1)  
fourth row=(88,55,22,33)  
fifth row=(88,55,3,1,5)

I want to select the rows which have 3 in their set. How can I do this?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
dave
  • 1,669
  • 5
  • 24
  • 32
  • duplicate of http://stackoverflow.com/questions/1987829/searching-from-comma-separated-value and of a number of others in http://stackoverflow.com/search?q=mysql+search+in+comma+separated+values – Dan D. Dec 14 '10 at 06:51
  • possible duplicate of [MySQL search in comma list](http://stackoverflow.com/questions/5458703/mysql-search-in-comma-list) – monsur.hoq May 03 '15 at 10:41

6 Answers6

24

Try:

SELECT * FROM TABLE_NAME WHERE FIND_IN_SET( 3, COLUMN_NAME ) 
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
Ajayendra
  • 231
  • 2
  • 2
13

How about something like

SELECT *
FROM Table
WHERE Field LIKE '3,%'
OR Field LIKE '%,3'
OR Field LIKE '%,3,%'
OR Field = '3'
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
3

Just Use Mysql Function FIND_IN_SET(str,strlist) .

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (“,”) character.

 SELECT * FROM table_name WHERE FIND_IN_SET('3', column_name);
1

use this condition

WHERE firstrow LIKE '3,%' 
    OR firstrow like '%,3'
    OR firstrow like '3'
Bhanu Prakash Pandey
  • 3,805
  • 1
  • 24
  • 16
1
SELECT *
FROM Table
WHERE Field IN ($first_row)
OR Field IN ($second_row)
OR Field IN ($third_row)
OR Field IN ($fourth_row);
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Chandresh
  • 11
  • 1
0

you can use IN statement in your query.Please try this.

SELECT *
FROM Table
WHERE Field IN ($first_row)
OR Field IN ($second_row)
OR Field IN ($third_row)
OR Field IN ($fourth_row);
Chandresh M
  • 3,808
  • 1
  • 24
  • 48