简体   繁体   English

MySql:使用IN运算符的Select语句

[英]MySql : Select statement using IN operator

I have a table with several fields and one field having 3 Comma Separated Values 我有一张表,其中包含多个字段,并且一个字段具有3个逗号分隔值

 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. 现在,我想运行一个SQL查询,该查询向我获取具有我作为输入发送的值的行。

Like in the above example, if i send a value 2 as an input to the function, it must return the 1st 2 rows. 像上面的示例一样,如果我将值2发送为函数的输入,则它必须返回1st 2行。

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运算符,但失败了,因为它不获取包含我作为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() : 您可以使用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: IN运算符基本上是许多OR的快捷方式:

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 <= 如果我正确理解了您的问题,然后尝试-从表中选择Column1,Column2,其中Column1 <=

cheers 干杯

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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