如何按月或年汇总日期Mysql

时间:2012-11-20 17:23:46

标签: mysql sum aggregate

$SQLString = "SELECT    
            count(score) as counts,
            DATE(date),
            SUM(CASE WHEN gender = 1 then 1 ELSE 0 END) Male,
            SUM(CASE WHEN gender = 2 then 1 ELSE 0 END) Female,
            AVG(age) as age, score
            FROM persons  
            WHERE date > '".$_SESSION['date1']."' AND date < '".$_SESSION['date2']."' 
            GROUP BY DATE(date) 
            ORDER BY DATE(date) asc";   

这是我的查询,显示一些数据,如性别,年龄等每天。 我怎样才能将这些日期按月或年汇总并显示添加的数据?

如果我不解释自己,请注意我:)

1 个答案:

答案 0 :(得分:4)

GROUP BY year(date), month(date)