显示使用MySQL返回零结果的行

时间:2013-05-07 18:44:57

标签: mysql sql

我正在为我们的一位客户撰写报告。该报告可以很好地报告每个区域每个课程的用户数,但是客户还希望显示具有零用户结果的课程。以下是一个例子:

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

2 个答案:

答案 0 :(得分: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

等等。我最好的猜测是,没有用户的课程没有“polltrack”(无论是什么;-)。因此,它们最终会从整体联接结果中遗漏。

另外,我希望您理解,当您在where子句中放置任何表格时,可能会将left join转换为inner join

答案 1 :(得分:0)

您离开的子查询中的所有行的计数是否与主表连接。您可以使用主查询中的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
相关问题