以validclick为分组,统计每个分组的记录数,和每个分组的totalclick总和

SELECT count(`id`),sum(`totalclick`), validclick FROM diary_log GROUP BY validclick;
count(`id`) sum(`totalclick`) validclick

配合if,可以模拟count()统计每个分组的记录数

SELECT count(`id`), sum(if(validclick = 1,1,0)) validclick_1,sum(if(validclick = 2,1,0)) validclick_2,validclick FROM diary_log  GROUP BY validclick;
count(`id`) validclick_1 validclick_2 validclick

对于count中应用if,则在第二第三个参数中只能赋值true和null:

SELECT COUNT(IF(`validclick`>2,TRUE,NULL)) AS `exceed_1_row_count` FROM diary_log;

错误的示范,这样做的话无论`validclick`是否大于2都会被计算:

SELECT COUNT(IF(`validclick`>2,1,0)) AS `exceed_1_row_count` FROM diary_log;