Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT mysql column with comma separated values

Tags:

mysql

Consider I have a table like this.

+-------------+---------------+----------------+---------------+
+     id      +    column 1   +    column 2    +    column 3   +
+-------------+---------------+----------------+---------------+ 
+      1      +     values    +     values     + a, b, c, d, e +  
+-------------+---------------+----------------+---------------+

My query can be like this

SELECT * FROM table WHERE column 1 = 'values' AND column 3 = '$variable'

The $variable can be anything from a to e

Is there a way to use query to select the row the I want?

like image 554
Gokul Gopala Krishnan Avatar asked Dec 21 '22 03:12

Gokul Gopala Krishnan


2 Answers

use FIND_IN_SET() -- a builtin function for mysql to search a string

SELECT  *
FROM    tableName
WHERE column 1 = 'values' and  FIND_IN_SET('a',column 3 )>0
  • sample

  • REFER

like image 93
Ganesh Rengarajan Avatar answered Dec 22 '22 16:12

Ganesh Rengarajan


Try this

SELECT  *
FROM    table
WHERE   FIND_IN_SET('$variable', column 3)
like image 33
Napster Avatar answered Dec 22 '22 16:12

Napster