简体   繁体   English

用IN选择会产生错误的结果

[英]select with IN gives wrong result

Field editors value '3,2,4' 现场编辑者将value '3,2,4'

sql statement sql语句

SELECT * FROM `table` where '3' in (editors)

result 1 row = correct 结果1行=正确

SELECT * FROM `table` where '2' in (editors)

result 0 row = not correct. 结果0行=不正确。

I presume the value '3,2,4' is treated as a string, how to solve this? 我假设value '3,2,4'被当作​​一个字符串,如何解决呢?

You'll have to use FIND_IN_SET() , or a hideous construct like 您必须使用FIND_IN_SET()或类似的可怕构造

  WHERE editors='2' 
    OR editors LIKE '2,%'
    OR editors LIKE '%,2,%'
    OR editors LIKE '%,2'

Right now your query is being interpreted as 现在,您的查询被解释为

 WHERE '2' IN ('1,2,3')
 a.k.a
 WHERE '2' = '1,2,3'

and not 并不是

 WHERE '2' IN ('1', '2', '3')
 a.k.a.
 WHERE '2' = '1' OR '2' = '2' OR '2' = '3'

like you want it to. 就像你想要的那样。 WHERE 哪里

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

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