一、連接兩個查詢結果
left join 是left outer join的簡寫,left join可以連接2個查詢結果集。我們通常的用法
SELECT S.STUDENT_NAME STUDENT_NAME, S.STUDENT_NO STUDENT_NO, F.ADDRESS ADDRESS FROM( SELECT G.GRADE_NO GRADE_NO, A.ADDRESS FROM GRADE G, ADDRESS A WHERE G.GRADE_NO = A.GRADE_NO ) F LEFT JOIN STUDENT S ON F.STUDENT_NO = S.STUDENT_NO
這是我們最常見的用法,基本上這種連接都能滿足一般的查詢需求。這里還要說明
一下,LEFT JOIN是先連接,后過濾。也就是說,在WHERE條件過濾之前,已經進行了連
接。假如我們現在有這樣一種場景,我們需要連接STUDENT表中的所有2016屆的學生,
而不是連接STUDENT表中所有的記錄。那么我們就需要在連接之前查詢出來所有2016屆
的學生。這種場景,left join也能輕松搞定。如下:
SELECT P.STUDENT_NAME STUDENT_NAME, P.STUDENT_NO STUDENT_NO, F.ADDRESS ADDRESS FROM( SELECT G.GRADE_NO GRADE_NO, A.ADDRESS FROM GRADE G, ADDRESS A WHERE G.GRADE_NO = A.GRADE_NO ) F LEFT JOIN ( SELECT S.STUDENT_NO STUDENT_NO, S.STUDENT_NAME STUDENT_NAME FROM STUDENT S WHERE S.YEAR = '2016' )P ON P.STUDENT_NO = F.STUDENT_NO
————————————————
版權聲明:本文為CSDN博主「春秋戰國程序猿」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/reggergdsg/article/details/66475187
二、連接多個查詢結果
項目中遇到這樣的問題,需要按指定的時間區間從10個數據庫表中查詢到關於10個司法局的業務數量,案件數量等11列信息。再對這些結果排序。
那么我們怎么通過一個SQL語句來解決呢?
下面介紹下SQL方法:
select a.*, a.id, b.id, c.price from author a left join book b on a.id=b.id left join order c on a.id=c.id
使用Left join -on語句將3張表鏈接到了一起。對於項目的解決辦法,可以將book,order,auther替換成sql語句,即select出來的表,再做一次left join
SQL語句如下
1 select
2 a.MEDIATIONCOMMITTEE,
3 a.tiaojieaj,
4 b.shangbaosl,
5 c.paichafk,
6 d.zixunrz,
7 e.chunjufw,
8 f.faxuanhd
9 from (select MEDIATIONCOMMITTEE,count(1) as tiaojieaj from MBM_CASE where DATEACCEPTED <= to_date('2018-05-10 00:00:00','yyyy-mm-dd hh24:mi:ss') group by MEDIATIONCOMMITTEE) a
10 left join (select MEDIATIONCOMMITTEE,count(1) as shangbaosl from MCS_MEDIATIONCASE where REPORTDATE between to_date('2016-01-10 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date
('2018-05-10 00:00:00','yyyy-mm-dd hh24:mi:ss') and STATUS !='ToBeReported' group by MEDIATIONCOMMITTEE) b
11 on a.MEDIATIONCOMMITTEE=b.MEDIATIONCOMMITTEE
12 left join (select MEDIATIONCOMMITTEE,count(1) as paichafk from CDS_INVESTIGATIONFEEDBAC where DATE_ between to_date('2016-01-10 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date
('2018-05-10 00:00:00','yyyy-mm-dd hh24:mi:ss') group by MEDIATIONCOMMITTEE) c
13 on a.MEDIATIONCOMMITTEE=c.MEDIATIONCOMMITTEE
14 left join (select MEDIATIONCOMMITTEE,count(1) as zixunrz from AMS_VILLAGESERVICELOG where CREATEDATE between to_date('2016-01-10 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date
('2018-05-10 00:00:00','yyyy-mm-dd hh24:mi:ss') group by MEDIATIONCOMMITTEE ) d
15 on a.MEDIATIONCOMMITTEE=d.MEDIATIONCOMMITTEE
16 left join (select MEDIATIONCOMMITTEE,count(1) as chunjufw from WWS_CONSULT where DATE_ between to_date('2016-01-10 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date
('2018-05-10 00:00:00','yyyy-mm-dd hh24:mi:ss') group by MEDIATIONCOMMITTEE ) e
17 on a.MEDIATIONCOMMITTEE=e.MEDIATIONCOMMITTEE
18 left join (select MEDIATIONCOMMITTEE,count(1) as faxuanhd from WWS_LEGALACTIVITY where STARTDATE between to_date('2016-01-10 00:00:00','yyyy-mm-dd hh24:mi:ss')
and to_date('2018-05-10 00:00:00','yyyy-mm-dd hh24:mi:ss') group by MEDIATIONCOMMITTEE) f
19 on a.MEDIATIONCOMMITTEE=f.MEDIATIONCOMMITTEE
————————————————
版權聲明:本文為CSDN博主「adam-liu」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_41664845/article/details/80763112
