在MySQL查询中使用now()函数


Using now() function in MySQL query

我有一个表与以下结构:

ITEMS ('id' int(10), 'stock' int(10), 'date_updated' DATETIME);

id是主键。

我的表的一些示例行是:

id  stock  date_updated
--  -----  -------------------
1   50     2011-11-05 05:00:00
2   40     2011-11-04 05:00:00
3   30     2011-11-03 05:00:00
4   20     2011-11-02 05:00:00

假设现在的时间是:2011-11-06 04:00:00

问题:找出最近2天内所有有date_updated的库存的总和。

示例:如果我现在运行查询,那么它应该输出90即50+40—id = 1id = 2作为这些行在过去2天内具有date_updated列的行之和。

我正在尝试以下查询,但没有成功。请帮助!

SELECT sum(stock)
FROM items
WHERE date_updated > now() - 2;

我在Windows机器上使用PHP, MySQL, Apache。

Use INTERVAL:

SELECT SUM(stock)
FROM items
WHERE date_updated > NOW() - INTERVAL 2 DAY