条件不起作用的MySQL IN子句

时间:2014-11-01 11:20:12

标签: php mysql

我有一个带有名称产品的表,其中一个列的名称是design_id,其中设计ID以逗号分隔格式保存,如 - 5,4,6,51,85,65,98,32,324,122,14,755,

我必须找到设计ID为98的结果。为此,我写了一个查询

SELECT * FROM `products` where `design_id` IN (98)

查询是否成功运行但未返回任何结果?

列类型为varchar。

3 个答案:

答案 0 :(得分:1)

IN运算符遍历数据集,因此您必须反转查询。

SELECT * FROM table_name WHERE 98 IN column_name

然而,在大多数数据库中,相等的opterator要快得多。

SELECT * FROM table_name WHERE column_name=98

答案 1 :(得分:1)

最好用于SELECT * FROM table_name where column_name='98'

但是你想存储:

尝试使用 INSERT

INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);

但是你想要获取:

尝试使用 mysql_fetch_row

示例:

 <?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";

if ($result=mysqli_query($con,$sql))
  {
  // Fetch one and one row
  while ($row=mysqli_fetch_row($result))
    {
    printf ("%s (%s)\n",$row[0],$row[1]);
    }
  // Free result set
  mysqli_free_result($result);
}

mysqli_close($con);
?>  

答案 2 :(得分:1)

示例:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";

if ($result=mysqli_query($con,$sql))
  {
  // Fetch one and one row
  while ($row=mysqli_fetch_row($result))
    {
    printf ("%s (%s)\n",$row[0],$row[1]);
    }
  // Free result set
  mysqli_free_result($result);
}

mysqli_close($con);
?>