MySql: Select operator using IN operator

I have a table with multiple fields and one field with three comma separated values

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

      

Now I want to run a SQL query that fetches me rows that have a value that I am sending as input.

As in the example above, if I send the value 2 as input to the function, it should return the 1st 2 lines.

I tried to use the IN operator but couldn't, as it doesn't fetch rows that have an i input, which you send as 2nd or 3rd CSV value. In this example, it does not return the 1st row.

Can someone please help me with this?

Thanks, Advance, Akasha

+1


a source to share


3 answers


You can use FIND_IN_SET()

:

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

      

The operator IN

is basically a shortcut to the OR set:



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

      

gives the same result as

WHERE (column2 = 'a' OR column2 = 'b' OR column2 = 'c')

      

+3


a source


If I understand your question correctly, then how about trying - select Column1, Column2 from the table where Column1 <=



amuses

0


a source


You should be able to use How to do this, for example:

SELECT * FROM tablename WHERE [Column 2] LIKE ("%2%");

      

0


a source







All Articles