简体   繁体   English

为什么这个MySQL查询只返回1个结果?

[英]Why Does This MySQL Query Return Just 1 Result?

Is the syntax right? 语法合适吗?

SELECT * 
  FROM productOptions 
 WHERE productOptionsID IN ('1,2,3,4')

Have I used IN properly, or should the comma separated values be different? 我是否正确使用了IN,或者逗号分隔值是否应该不同?

This is followed by the following code: 接下来是以下代码:

$optionsresultsql= mysql_query($optionsquerysql) or die(mysql_error());

while($optionssql = mysql_fetch_array($optionsresultsql)) {
  $optionNamesID = $optionssql["optionNamesID"];
  echo $optionNamesID;
}   

Only one result is shown, even though there are 4 matches in the DB. 即使DB中有4个匹配项,也只显示一个结果。

处理INT查找时删除单一滴答。

"SELECT * FROM productOptions WHERE productOptionsID IN (1,2,3,4)"

'1,2,3,4' is a string, which is being converted to an int. '1,2,3,4'是一个字符串,正在转换为int。 MySQL converts strings to ints by reading up until the 1st non-number character, in your case, the , . MySQL通过读取直到第一个非数字字符(在您的情况下为,来将字符串转换为整数。

So, 所以,

IN ('1,2,3,4') = IN (CAST('1,2,3,4' AS INT)) = IN (1)

The query should just be: 查询应该只是:

productOptionsID IN (1,2,3,4)

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

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