MySQL 使用 3 个表进行左连接、计数和分组的查询
MySQL query with left join, count and group with 3 tables
我有 3 个 table 试图放置如下所示的连接查询。
下面是包含 3 个部分的 table 部分。
Section
*****************************
* section_id * section_name *
*****************************
* 1 * A *
* 2 * B *
* 3 * C *
*****************************
下面是section_subjecttable。第一节包含 2 个科目,第二节包含 2 个科目,第三节包含 3 个科目。
Section_Subject
***********************************
* ss_id * section_id * subject_id *
***********************************
* 1 * 1 * 8 *
* 2 * 1 * 9 *
* 3 * 2 * 6 *
* 4 * 2 * 5 *
* 5 * 3 * 2 *
* 6 * 3 * 3 *
* 7 * 3 * 4 *
***********************************
下面是section_batchtable。仅第 3 部分包含 2 个批次
Section_Batch
*********************************
* sb_id * section_id * batch_id *
*********************************
* 1 * 3 * 6 *
* 2 * 3 * 7 *
*********************************
我希望查询产生以下结果
**************************************************************
* section_id * section_name * count_subjects * count_batches *
**************************************************************
* 1 * A * 2 * 0 *
* 2 * B * 2 * 0 *
* 3 * C * 3 * 2 *
**************************************************************
我知道我们可以做一些子查询来达到上面的结果。但是如何使用left join和group query得到结果呢?
您可以将 count
与 distinct
一起使用:
select t1.section_id
, t1.section_name
, count(distinct t2.subject_id) as count_subjects
, count(distinct t3.batch_id) as count_batches
from Section t1
left join Section_Subject t2 on t1.section_id = t2.section_id
left join Section_Batch t3 on t1.section_id = t3.section_id
group by t1.section_id
, t1.section_name
您需要分别执行 left join
和 group by
table 以获得计数,然后在它们之间进行连接
SQL Fiddle: http://www.sqlfiddle.com/#!9/ea4ee/12
select T1.section_id,
T1.section_name,
T1.subjects_count,
T2.batch_count
FROM (
select S.section_id,
S.section_name,
COUNT(SS.subject_id) as subjects_count
from Section S
LEFT JOIN Section_Subject SS
on S.section_id = SS.section_id
group by S.section_id, S.section_name )T1
LEFT JOIN (
select S.section_id,
S.section_name,
COUNT(SB.batch_id ) as batch_count
from Section S
LEFT JOIN Section_Batch SB
on S.section_id = SB.section_id
group by S.section_id, S.section_name
) T2
on T1.section_id = T2.section_id
我相信使用 count(distinct)
可以满足您的需求。您必须使用 distinct
,因为连接具有乘数效应,其中一个部分有多个主题和多个批次。
select
s.section_id,
min(t1.section_name) as section_name,
count(distinct ss.subject_id) as subject_count,
count(distinct sb.batch_id) as batch_count,
from
Section as s
left join Section_Subject as ss on ss.section_id = s.section_id
left join Section_Batch as sb on sb.section_id = s.section_id
group by
s.section_id
顺便说一句,我认为左连接可能是内部连接。
我有 3 个 table 试图放置如下所示的连接查询。
下面是包含 3 个部分的 table 部分。
Section
*****************************
* section_id * section_name *
*****************************
* 1 * A *
* 2 * B *
* 3 * C *
*****************************
下面是section_subjecttable。第一节包含 2 个科目,第二节包含 2 个科目,第三节包含 3 个科目。
Section_Subject
***********************************
* ss_id * section_id * subject_id *
***********************************
* 1 * 1 * 8 *
* 2 * 1 * 9 *
* 3 * 2 * 6 *
* 4 * 2 * 5 *
* 5 * 3 * 2 *
* 6 * 3 * 3 *
* 7 * 3 * 4 *
***********************************
下面是section_batchtable。仅第 3 部分包含 2 个批次
Section_Batch
*********************************
* sb_id * section_id * batch_id *
*********************************
* 1 * 3 * 6 *
* 2 * 3 * 7 *
*********************************
我希望查询产生以下结果
**************************************************************
* section_id * section_name * count_subjects * count_batches *
**************************************************************
* 1 * A * 2 * 0 *
* 2 * B * 2 * 0 *
* 3 * C * 3 * 2 *
**************************************************************
我知道我们可以做一些子查询来达到上面的结果。但是如何使用left join和group query得到结果呢?
您可以将 count
与 distinct
一起使用:
select t1.section_id
, t1.section_name
, count(distinct t2.subject_id) as count_subjects
, count(distinct t3.batch_id) as count_batches
from Section t1
left join Section_Subject t2 on t1.section_id = t2.section_id
left join Section_Batch t3 on t1.section_id = t3.section_id
group by t1.section_id
, t1.section_name
您需要分别执行 left join
和 group by
table 以获得计数,然后在它们之间进行连接
SQL Fiddle: http://www.sqlfiddle.com/#!9/ea4ee/12
select T1.section_id,
T1.section_name,
T1.subjects_count,
T2.batch_count
FROM (
select S.section_id,
S.section_name,
COUNT(SS.subject_id) as subjects_count
from Section S
LEFT JOIN Section_Subject SS
on S.section_id = SS.section_id
group by S.section_id, S.section_name )T1
LEFT JOIN (
select S.section_id,
S.section_name,
COUNT(SB.batch_id ) as batch_count
from Section S
LEFT JOIN Section_Batch SB
on S.section_id = SB.section_id
group by S.section_id, S.section_name
) T2
on T1.section_id = T2.section_id
我相信使用 count(distinct)
可以满足您的需求。您必须使用 distinct
,因为连接具有乘数效应,其中一个部分有多个主题和多个批次。
select
s.section_id,
min(t1.section_name) as section_name,
count(distinct ss.subject_id) as subject_count,
count(distinct sb.batch_id) as batch_count,
from
Section as s
left join Section_Subject as ss on ss.section_id = s.section_id
left join Section_Batch as sb on sb.section_id = s.section_id
group by
s.section_id
顺便说一句,我认为左连接可能是内部连接。