create 24hour top table by pageview
how would i create a "last 24 hour top" table by pageviews if i have the following tables.
movie: id, title
pageview: id, movie, vieweddate
i need the third table like this
top: id, movie, moviecount24
so i would be able to make such query:
select * from to开发者_如何学Gop24 order by viewcount24 desc limit 30
i need to create top
table using ONE mysql query.
select
pageview.id as pageview_id
,movie.title as move_title
,count(movie.id) as moviecount24
from pageview
inner join movie on (pageview.movie = movie.id)
where pageview.vieweddate between date_sub(now(), interval 1 day) and now()
order by moviecount24 desc
limit 30;
Let me refactor Johan's response a bit:
INSERT INTO top
SELECT movie.id AS id, COUNT(movie.id) as moviecount24
FROM pageview
LEFT JOIN movie ON pageview.movie = movie.id
WHERE vieweddate > NOW() - INTERVAL 1 DAY
LEFT JOIN
is important here, since some movies may have zero views, but still should be present in the result.
精彩评论