tags:

views:

909

answers:

3

Hi,

I have a table with several fields and one field having 3 Comma Separated Values

 Column 1 Column 2 
    1        1,2,3
    2        2,3,4
    3        6,7,8

Now i want to run an SQL query which fetches me the rows which have the value i send as an input.

Like in the above example, if i send a value 2 as an input to the function, it must return the 1st 2 rows.

I tried using the IN operator but failed as it does not fetch the rows which have the input i send as the 2nd or 3rd value of the CSV. In this example, it does not return the 1st row.

Can someone please help me out with this?

Thanks in Advance, Akash

+2  A: 

You can use FIND_IN_SET():

SELECT column1 FROM table WHERE FIND_IN_SET('2', column2) != 0

The IN operator is basically a shortcut for lots of ORs:

WHERE column2 = IN ('a', 'b', 'c')

gives the same result as

WHERE (column2 = 'a' OR column2 = 'b' OR column2 = 'c')
Greg
Thank You so much for the quick reply.
A: 

Hi

If I have understood your question correctly then how about trying - select Column1, Column2 from Table where Column1 <=

cheers

Andriyev
A: 

You should be able to use Like to do this, something like:

SELECT * FROM tablename WHERE [Column 2] LIKE ("%2%");
Jona
This will only work if all the numbers are single-digits otherwise it will match e.g. 12 or 21
Greg
True, although you could use a more complex pattern to fix that e.g. "%,2,%" or depending on how the numbering works only match the wildcard on one side.
Jona