raghuveer999
raghuveer999

Reputation: 709

SQL Query to get a row which has comma separated values

I have a SQL Table which has rows like this

--------------------
 id   promocode
--------------------
 1    test,test2
 2    test,test3
 3    test2,test3

Now I want to select rows which has value 'test' in the 'promocode' column.
When I use

SELECT id from table WHERE promocode LIKE 'test'

it is returning all the rows because all the rows have the string 'test' in them. But I want only 1 & 2 rows because only those two rows have the exact value I need. Can anyone help me with the query?

Upvotes: 0

Views: 932

Answers (1)

VMai
VMai

Reputation: 10336

You can use the function FIND_IN_SET:

SELECT id from table WHERE FIND_IN_SET('test', promocode) > 0

Demo

Note:

Storing comma separated lists in columns is almost ever asking for trouble. Please consider normalizing your database.

Upvotes: 6

Related Questions