SET @today=CAST(NOW() AS CHAR(64)) ,
@yesterday=CAST(DATE_ADD(NOW(), INTERVAL -1 DAY) AS DATE) ;
select sum(quantity)
from orders o,product p,order2product op
where o.order_id = op.order_id AND op.product_id = p.product_id AND order_date < @today AND order_date > @yesterday
它给出了以下
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select sum(quantity)from orders o,product p,order2product op where o.order_id = ' at line 1
但是当我直接在mysql中使用它时工作正常。问题是什么?
更新
$sql = mysql_query("SET @today=CAST(NOW() AS CHAR(64)),@yesterday=CAST(DATE_ADD(NOW(),INTERVAL - 1 DAY) AS DATE); select sum(quantity)from orders o,product p,order2product op where o.order_id = op.order_id AND op.product_id = p.product_id AND order_date < @today AND order_date > @yesterday") or die(mysql_error());
$existCount = mysql_num_rows($sql); // count the row nums.
if($existCount > 0)
{
while($row = mysql_fetch_array($sql))
{
$dayQuantity = $row["sum(quantity)"];
}
}