SELECT date, id, sum(revenue)
FROM table
WHERE date between '2013-01-01' and '2013-01-08'
GROUP BY date, id
HAVING sum(revenue)>1000
返回收入> 1000的行。按列聚合高效分组
SELECT date, id, sum(revenue)
FROM table
WHERE date between '2013-01-01' and '2013-01-08'
AND id IN (SELECT id FROM table where date between '2013-01-01' and '2013-01-08' GROUP BY id HAVING sum(revenue)>1000)
GROUP BY date, id
根据需要返回id在日期期间总收入大于1000的行。但是这个查询要慢得多。任何更快的方法来做到这一点?
怎么样'EXPLAIN'第一? – zerkms
@ChrisArmstrong这些查询返回不同的结果吗? –