对于下面的查询,我如何计算datesent
小于24小时的行数? (字段datesent
是时间戳)。
提前致谢,
约翰
$message = "SELECT datesent, recipient
FROM privatemessage
WHERE recipient = '$u'";
$messager = mysql_query($message);
$messagearray = array();
答案 0 :(得分:56)
使用:
SELECT COUNT(*) AS cnt
FROM PRIVATEMESSAGE pm
WHERE pm.datesent >= DATE_SUB(NOW(), INTERVAL 1 DAY)
答案 1 :(得分:6)
答案 2 :(得分:3)
$message="select count(1)
from privatemessage
where datesent>=date_sub(now(), 24 hours)
and recipient='$u'"
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
答案 3 :(得分:1)
我认为这是一个旧的,但对于后来通过在线搜索到达此处的任何人,我想澄清问题是关于时间戳,而不是日期时间。我需要一个基于时间戳的计数。简单的解决方法是将datetime转换为unix_timestamp。在MySQL 5.7上测试
SELECT COUNT(*) AS cnt
FROM PRIVATEMESSAGE pm
WHERE pm.datesent >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))