The table is something like the following:
date name count
2013-1-1 Jack 20
2014-3-8 Jack 3
2014-3-1 Tom 1
2014-3-1 Jack 7
2014-2-28 Mary 4
2014-2-28 Tom 5
I want to write a query to output the person with their total counts within last 30 days
eg.
Jack 10
Tom 6
Mary 4
What is the simplest SQL to do this?
解决方案
Update: Query as suggested by @MatBailie
mysql> SELECT
-> name,
-> SUM(count) as total_count
-> FROM table_name
-> WHERE `date` >= DATE_SUB(NOW(), INTERVAL 30 day) AND `date` <= NOW()
-> GROUP BY name
-> ;
+------+-------------+
| name | total_count |
+------+-------------+
| Jack | 10 |