I am trying to filter some results for the last 24 hours only. I have timestamps in the time column and came up with this but it doesn't filter for last 24 hours it shows everything.
$last_day = "SELECT site_id, time, COUNT(*) AS site_num FROM url_visits WHERE time < DATE_SUB( NOW(), INTERVAL 24 HOUR) GROUP BY site_id ORDER by COUNT(*) DESC";
Does anyone know how I can make this query work for the last 24 hours? It works correctly for the grouping etc just not the last 24 hours. Thanks.
EDIT:
Here is what ended up working and the only solution I found to work in case anyone ever comes across this and are looking to do the same thing.
SELECT site_id, time, COUNT(*) AS site_num FROM url_visits WHERE time > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY site_id ORDER by COUNT(*) DESC
Try using
time > DATE_SUB( NOW(), INTERVAL 24 HOUR)
rather than
time < DATE_SUB( NOW(), INTERVAL 24 HOUR)
$last_day = "SELECT site_id, time, COUNT(*) AS site_num FROM url_visits WHERE time > DATE_SUB( NOW(), INTERVAL 24 HOUR) GROUP BY site_id ORDER by COUNT(*) DESC";
更改了比较
put a - infront of the 24. You want where time > NOW - 24 hrs. not + 24 hrs. DATE_SUB( NOW(), INTERVAL -24 HOUR)
.
OR you need to add 24 hrs to your time and say now < time + 24 hrs
Here is what I ended up using and the only thing that worked. Hoped this helps someone else.
SELECT site_id, time, COUNT(*) AS site_num FROM url_visits WHERE time > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY site_id ORDER by COUNT(*) DESC
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.