簡體   English   中英

MySQL查詢具有左聯接,計數和3個表分組

[英]MySQL query with left join, count and group with 3 tables

我有3個表,試圖在其中放入如下所示的聯接查詢。

以下是包含3個部分的Section表。

Section
*****************************
* section_id * section_name *
*****************************
*  1         * A            *
*  2         * B            *
*  3         * C            *
*****************************

下面是section_subject表。 第一部分包含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_batch表。 第三部分僅包含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 by以獲取計數,然后在它們之間進行聯接

SQL小提琴: 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

您可以將countdistinct一起使用:

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

SQLFiddle

我相信使用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

順便說一句,我認為左聯接可能是內部聯接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM