一个表中的多个计数UNINTENTIONALLY对行中的列进行求和

时间:2015-04-17 16:01:19

标签: mysql sql count mysql-workbench

我试图在休息期间将每天的点击次数显示在一个表格中。

SELECT module, COUNT( * ) 
FROM mdl_log AS l
WHERE (FROM_UNIXTIME( l.`time` ) BETWEEN  '2014-12-16 00:00:00' AND '2014-12-17 00:00:00')
GROUP BY module

准确地告诉我一天(第16次)需要的东西

module  Count(*)
admin   11
assign  1
course  20
outcome 2
user    3

但是当我尝试在同一张表中获得多天时,它会给我一行每行的总和。

SELECT module 

 ,(SELECT COUNT( * ) 
FROM mdl_log as l
WHERE (FROM_UNIXTIME( l.`time` ) BETWEEN  '2014-12-12 00:00:00' AND '2014-12-13 00:00:00')
) AS "12/12/2014"

,(SELECT COUNT( * ) 
FROM mdl_log AS l
WHERE (FROM_UNIXTIME( l.`time` ) BETWEEN  '2014-12-16 00:00:00' AND '2014-12-17 00:00:00')
) AS "12/16/14"

FROM mdl_log 
group by module 

此...

module 12/12/14  12/16/14
admin      1    37
assign     1    37
calendar   1    37
course     1    37
forum      1    37
grade      1    37
hsuforum   1    37
library    1    37
login      1    37
lti        1    37
message    1    37
outcome    1    37
question   1    37
quiz       1    37
role       1    37
url        1    37
user       1    37
webservice  1   37
workshop    1   37

你能给我任何关于如何获得这个的建议,以便它显示我想要的方式吗? 例如

module  12/12/14  12/16/14
admin   1         11
assign  0         1
user    0         3

1 个答案:

答案 0 :(得分:0)

count函数中应用日期标准:

SELECT module, 
COUNT(IF(FROM_UNIXTIME( l.`time` ) BETWEEN  '2014-12-12 00:00:00' AND '2014-12-13 00:00:00', 1, NULL) AS "12/12/2014",
COUNT(IF(FROM_UNIXTIME( l.`time` ) BETWEEN  '2014-12-16 00:00:00' AND '2014-12-17 00:00:00', 1, NULL) AS "12/16/2014"
FROM mdl_log 
group by module