MySql:使用IN运算符选择语句

时间:2022-11-30 07:43:43

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作为函数的输入,它必须返回前两行。

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.

我尝试使用IN运算符但失败了,因为它没有获取具有作为CSV的第2或第3值发送的输入的行。在此示例中,它不返回第1行。

Can someone please help me out with this?

有人可以帮我解决这个问题吗?

Thanks in Advance, Akash

先谢谢,阿卡什

3 个解决方案

#1


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

#2


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

如果我已正确理解您的问题,那么如何尝试 - 从Table中选择Column1,Column2,其中Column1 <=

cheers

#3


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

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

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

#1


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

#2


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

如果我已正确理解您的问题,那么如何尝试 - 从Table中选择Column1,Column2,其中Column1 <=

cheers

#3


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

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

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