MySQL中的SQL Where子句

时间:2013-01-08 21:11:33

标签: php mysql

我想知道为什么这个where子句在MySql中不起作用?我将runDate列设置为数据类型“date”,日期显示在下面的格式中:

<?php
$con = mysql_connect("***","***","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ArnoldsPizzaShop", $con);




$result = mysql_query("SELECT * FROM runinfo WHERE runDate = 2013-01-06");


while($row = mysql_fetch_array($result))
  {
echo  $row['route'] ;
 }


mysql_close($con);
?>

1 个答案:

答案 0 :(得分:7)

您需要在日期值周围使用单引号:

SELECT * 
FROM runinfo 
WHERE runDate = '2013-01-06'

所以你的代码是:

mysql_query("SELECT * FROM runinfo WHERE runDate = '2013-01-06'");

如果您的runDate列也包含时间,那么您可能需要使用:

SELECT * 
FROM runinfo 
WHERE Date(runDate) = '2013-01-06'

甚至:

SELECT * 
FROM runinfo 
WHERE runDate between '2013-01-06' and '2013-01-07'