Code:
SELECT SUM(visitors) FROM all_visitors WHERE id >= (SELECT id from all_visitors ORDER BY id DESC limit 5,1)
As far as I know, you need a WHERE clause to narrow down the results for the SUM to work. The subquery grabs the fifth-to-last ID in the table, and the main query only show results greater than or equal to that ID.
A GROUP BY clause would probably narrow it down for you too, but I couldn't think of a good way to fit that in in this instance.
You could also just grab the last 5 entries without using SUM, and use a PHP loop to add them up.
Last edited by Llama Herder; 01-06-2010 at 04:03 PM..
|