开发者

Why Does This MySQL Query Return Just 1 Result?

开发者 https://www.devze.com 2023-02-14 12:43 出处:网络
Is the syntax right? SELECT * FROM productOp开发者_如何学JAVAtions WHERE productOptionsID IN (\'1,2,3,4\')

Is the syntax right?

SELECT * 
  FROM productOp开发者_如何学JAVAtions 
 WHERE productOptionsID IN ('1,2,3,4')

Have I used IN properly, or should the comma separated values be different?

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.


remove the single-ticks when dealing with INT lookups..

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


'1,2,3,4' is a string, which is being converted to an int. MySQL converts strings to ints by reading up until the 1st non-number character, in your case, the ,.

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

精彩评论

暂无评论...
验证码 换一张
取 消