I have a file upload site, and I want to run a maintenance script that will run every day and delete items that haven't been accessed in a week. I log views for each day, and each item into a table:
hit_itemid
hit_date
hit_views
The main table actually has the files that were uploaded, for the purposes of this example, its just vid_id
, vid_title
thats in this table, and vid_id
will equal to hit_itemid
.
I have a query as follows:
SELECT vid_id,
vid_title,
SUM(case when hit_date >= '2009-09-17' then hit_hits else 0 end) as total_hits
FROM videos
LEFT JOIN daily_hits ON vid_id = hit_itemid
WHERE vid_posttime <= '$last_week_timestamp' AND vid_status != 3
GROUP BY hit_itemid
HAVING total_hits < 1
But this always returns a single record.
How can I rewrite this query?