2013-05-07 25 views
0

我目前正在为我们的客户之一编写报告。该报告适用于报告每个地区每个课程的用户数量,但客户还希望有零用户结果的课程可以显示。下面是一个例子:显示使用MySQL返回零结果的行

SELECT 
lc.code as course_code, count(rc1.region_id) as sc1data, count(rc2.region_id) as 
sc2data, count(rc3.region_id) as sc3data, count(rc4.region_id) as sc4data, 
count(rc5.region_id) as sc5data, count(rc6.region_id) as sc6data, 
count(rc7.region_id) as sc7data, count(rc8.region_id) as sc8data, 
count(rc9.region_id) as sc9data, count(rc10.region_id) as sc10data, 
count(rc11.region_id) as sc11data, count(rc12.region_id) as sc_total_data 

FROM learning_polltrack lpt 
LEFT JOIN learning_organization lo ON lo.idOrg = lpt.id_reference 
LEFT JOIN learning_course lc ON lc.idCourse = lo.idCourse 
LEFT JOIN core_field_userentry cfue ON lpt.id_user = cfue.id_user AND cfue.id_common IN (26) 
LEFT JOIN core_field cf ON cf.idField = cfue.id_common 
LEFT JOIN core_field_son cfs ON cfs.idField = cfue.id_common AND cfue.user_entry = cfs.idSon 
LEFT JOIN provider_type_by_title ptbt ON cfs.translation = ptbt.title 
LEFT JOIN core_field_userentry cfue2 ON cfue.id_user = cfue2.id_user AND cfue2.id_common = '15' 
LEFT JOIN core_field_son cfs2 ON cfs2.idField = '15' AND cfs2.id_common_son = cfue2.user_entry 
LEFT JOIN region_by_county rc1 ON rc1.county = cfs2.translation AND rc1.region_id = 1 
LEFT JOIN region_by_county rc2 ON rc2.county = cfs2.translation AND rc2.region_id = 2 
LEFT JOIN region_by_county rc3 ON rc3.county = cfs2.translation AND rc3.region_id = 3 
LEFT JOIN region_by_county rc4 ON rc4.county = cfs2.translation AND rc4.region_id = 4 
LEFT JOIN region_by_county rc5 ON rc5.county = cfs2.translation AND rc5.region_id = 5 
LEFT JOIN region_by_county rc6 ON rc6.county = cfs2.translation AND rc6.region_id = 6 
LEFT JOIN region_by_county rc7 ON rc7.county = cfs2.translation AND rc7.region_id = 7 
LEFT JOIN region_by_county rc8 ON rc8.county = cfs2.translation AND rc8.region_id = 8 
LEFT JOIN region_by_county rc9 ON rc9.county = cfs2.translation AND rc9.region_id = 9 
LEFT JOIN region_by_county rc10 ON rc10.county = cfs2.translation AND rc10.region_id = 10 
LEFT JOIN region_by_county rc11 ON rc11.county = cfs2.translation AND rc11.region_id = 11 
LEFT JOIN region_by_county rc12 ON rc12.county = cfs2.translation 
WHERE ptbt.provider_type = $P{provider_type} AND lc.code NOT LIKE '6 Month%' AND lc.code NOT LIKE 'F2F%' AND lc.code NOT LIKE 'Beta%' AND lpt.date_attempt BETWEEN $P{From} AND $P{To} 

GROUP BY course_code ORDER BY course_code 

这是我的代码,结果是这样的:

course_code | region 1 | region 2 | region 3 | ...... | region 11 | TOTAL 
course 1 |  5 |  0 | 1  |  |  1  | 7 
course 2 |  2 |  1 | 0  |  |  1  | 4 
course 4 |  3 |  0 | 1  |  |  0  | 4 
course 8 |  1 |  0 | 0  |  |  0  | 1 

但我需要它看起来就像是:

course_code | region 1 | region 2 | region 3 | ...... | region 11 | TOTAL 
course 1 |  5 |  0 | 1  |  |  1  | 7 
course 2 |  2 |  1 | 0  |  |  1  | 4 
course 3 |  0 |  0 | 0  |  |  0  | 0 
course 4 |  3 |  0 | 1  |  |  0  | 4 
course 5 |  0 |  0 | 0  |  |  0  | 0 
course 6 |  0 |  0 | 0  |  |  0  | 0 
course 7 |  0 |  0 | 0  |  |  0  | 0 
course 8 |  1 |  0 | 0  |  |  0  | 1 

回答

0

执行COUNT您留下的子查询中的所有行与主表连接。您可以在主查询中使用sum(rc.region_id = N)来旋转区域ID。

SELECT 
    lc.code as course_code, 
    ifnull(sum(rc.region_id = 1), 0) sc1data, 
    ifnull(sum(rc.region_id = 2), 0) sc2data, 
    ..., 
    ifnull(sc_total_data, 0) sc_total_data 
FROM learning_polltrack lpt 
LEFT JOIN learning_organization lo ON lo.idOrg = lpt.id_reference 
LEFT JOIN learning_course lc ON lc.idCourse = lo.idCourse 
LEFT JOIN core_field_userentry cfue ON lpt.id_user = cfue.id_user AND cfue.id_common IN (26) 
LEFT JOIN core_field cf ON cf.idField = cfue.id_common 
LEFT JOIN core_field_son cfs ON cfs.idField = cfue.id_common AND cfue.user_entry = cfs.idSon 
LEFT JOIN provider_type_by_title ptbt ON cfs.translation = ptbt.title 
LEFT JOIN core_field_userentry cfue2 ON cfue.id_user = cfue2.id_user AND cfue2.id_common = '15' 
LEFT JOIN core_field_son cfs2 ON cfs2.idField = '15' AND cfs2.id_common_son = cfue2.user_entry 
LEFT JOIN region_by_county rc on rc.county = cfs2.translation 
LEFT JOIN (select county, count(*) sc_total_data from region_by_county group by region) rcall on rcall.county = cfs2.translation 
WHERE ptbt.provider_type = $P{provider_type} AND lc.code NOT LIKE '6 Month%' AND lc.code NOT LIKE 'F2F%' AND lc.code NOT LIKE 'Beta%' AND lpt.date_attempt BETWEEN $P{From} AND $P{To} 
GROUP BY course_code ORDER BY course_code 
+0

我都尝试你的答案既不做了区别。结果仍然不显示结果为0的行。 – dannycodes 2013-05-07 19:10:25

+0

尝试改写查询(你需要填写省略号) – Barmar 2013-05-07 19:19:52

+0

JOIN代替LEFT JOIN约LEFT OUTER什么? – Lisa 2013-05-07 19:20:33

1

如果你想要所有的课程,然后开始链接left outer join与课程列表。我认为这是别名lc的表格。取而代之的是:

FROM learning_polltrack lpt 
LEFT JOIN learning_organization lo ON lo.idOrg = lpt.id_reference 
LEFT JOIN learning_course lc ON lc.idCourse = lo.idCourse 

试试这个:

from learning_course lc left outer join 
    learning_organizatino lo 
    on lc.idCourse = lo.idCourse left outer join 
    learning_polltrack lpt 
    on lo.idOrg = lpt.id_reference 

等。我最好的猜测是没有用户的课程没有“投票轨道”(不管那是什么;-)。所以,他们最终失去了整体联结结果。

另外,我希望你明白,当你在where条款中放置任何表格时,它会冒着将left join变成inner join的风险。

+0

此更改无效= /结果保持不变。 – dannycodes 2013-05-07 21:00:04

+0

@dannycodes。 。 。这使'ptbt.provider_type'和'lpt.date_attempt'上的'where'条件成为可能的过滤掉没有用户的课程的罪魁祸首。 – 2013-05-07 21:11:34