Using PHP and MySQL.
My DB structure is like so:
id, user, car, view_stamp
view_stamp is a TIMESTAMP like so: 2014-02-05 11:11:47
What I want to do is get the ' user
' that has over 100 views in any given one hour span, in the past 24 hours.
This is my failed attempt at a loop query:
SELECT user, COUNT(id) AS TotalViews
FROM car_views
WHERE TotalViews > 100 AND view_stamp > DATE_SUB(NOW(),INTERVAL 1 DAY)
ORDER BY TotalViews DESC
LIMIT 10
The above does not have the 'One hour Span' requirement, and I don't know how to include it. It sounds confusing I'm sure, So let me try and rephrase just in case I made no sense above...
The time to check is just the past 24 hours. Within that 24 hour span, I need to know if any user viewed more than 100 times in any given 1 hour span.
Thanks for any help. If I am missing any useful info, please let me know and I will edit.
You need to use GROUP BY
to get per-user, per-hour totals. And you have to use HAVING
to test aggregate data.
SELECT user, HOUR(view_stamp) AS the_hour, COUNT(*) AS hourly_views
FROM car_views
WHERE view_stamp > DATE_SUB(NOW(), INTERVAL 1 DAY)
GROUP BY user, the_hour
HAVING hourly_views > 100
ORDER BY hourly_views DESC
LIMIT 10
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.