简体   繁体   中英

MySql : Select statement using IN operator

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

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')

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

cheers

您应该可以使用Like来执行此操作,例如:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM