数据接收时mysql查询问题

时间:2014-05-29 05:45:00

标签: mysql sql

使用表##

检索数据的SQL查询

我需要查询才能获得每个级别的参与者数量。

注意:第4级的参与者不应该处于其他级别,例如:Level 3,2,1

表:

ID   Level    date

38  1 06 -05
38  2 08 -05
38  3 12 -05
38  4 13 -05 
39  1 13 -05
39  2 13 -05
40  1 12 -05

需要输出:

Count  Level
 1      1
 1      2
 0      3
 1      4

2 个答案:

答案 0 :(得分:0)

您可以定义一个artadditional表(或创建一个子查询),它将返回30行 e.g

(select now() as d
union
select date_sub(now(), interval 1 day) as d
union
select date_sub(now(), interval 2 day) as d
union
...
select date_sub(now(), interval 29 day) as d) all_dates_source

然后您可以将查询结果LEFT JOIN到all_dates_source

SELECT i,
       Logindate2,
       ifnull(Logindate2,0, Logins2) as Logins2
FROM (...) all_dates_source
   LEFT JOIN (yourQueryHere) sourceQuery 
        on all_dates_source.d=sourceQuery.Logindate2  

答案 1 :(得分:0)

SELECT count(t2.ppt) as count,t1.Level as level
FROM courselevel t1
LEFT JOIN
(SELECT ppt,MAX(Level) as Level
FROM courselevel
GROUP BY ppt ) t2 on t1.ppt=t2.ppt and t1.Level=t2.Level
GROUP BY t1.Level