MySql:使用IN运算符的Select语句 [英] MySql : Select statement using IN operator

查看:72
本文介绍了MySql:使用IN运算符的Select语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个字段的表格和一个具有3个逗号分隔值的字段。

 第1列第2列
1 1,2,3
2 2,3,4
3 6,7,8

现在我想运行一个SQL查询,它获取我发送的值作为输入的行。



像上面的例子一样,if我发送一个值2作为输入到函数,
它必须返回第1 2行。



我尝试使用IN操作符,但失败,因为它而不是获取我输入的行作为CSV的第2或第3个值。在这个例子中,它不返回第一行。



有人可以帮我吗?



您可以使用 FIND_IN_SET() c $ c>:

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

IN 操作符基本上是许多OR的快捷方式: / p>

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

给出与

相同的结果

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


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

这篇关于MySql:使用IN运算符的Select语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆