如何仅从昨天开始选择我的sql数据库?这是我的代码:

      SELECT *, COUNT(visitors.usr_id) as usr_count FROM user, visitors WHERE visitors.usr_id = $usr_id GROUP BY $usr_id ORDER BY usr_count LIMIT 1


我的数据库名称是timein,它是输入INSERT完成日期和时间的方式。这是代码:

      Database column :    timein

      Database insert looks like :    2012-9-6 9:11:35


基本上,我希望只能从昨天开始进行SELECT和COUNT。我怎么只能从昨天的SQL数据库中选择COUNT?

最佳答案

试试这个添加昨天的条件,例如timein <= NOW() - INTERVAL 1 DAY , timein >= NOW() - INTERVAL 2 DAY;

SELECT *, COUNT(visitors.usr_id) as usr_count
FROM user, visitors
WHERE visitors.usr_id = $usr_id
AND timein <= NOW() - INTERVAL 1 DAY
AND timein >= NOW() - INTERVAL 2 DAY
GROUP BY $usr_id
ORDER BY usr_count
LIMIT 1


或者您可以使用BETWEEN

SELECT *, COUNT(visitors.usr_id) as usr_count
FROM user, visitors
WHERE visitors.usr_id = $usr_id
AND timein BETWEEN  NOW() - INTERVAL 1 DAY AND  NOW() - INTERVAL 2 DAY;
GROUP BY $usr_id
ORDER BY usr_count
LIMIT 1

09-12 13:39