sql中join與left-join圖解區別

 
  select a.*  from YG_BRSYK a 
  left join(SELECT DISTINCT SYXH,
                        STUFF((SELECT '、'+MS FROM #lsb where SYXH=t.SYXH FOR XML PATH('')),1,1,'') AS MS
                        FROM #lsb as t) c on a.SYXH=c.SYXH WHERE c.MS IS NOT NULL order by RYBQ 
--注:left join...on 爲左關聯,保留左邊全部的數據,右表沒有的數據爲NULL值。加where條件控制表的輸出C.MS不爲NULL。所以查詢的個數與c表一致760個

 

  select a.*  from YG_BRSYK a --801331個

 

SELECT DISTINCT SYXH,
                        STUFF((SELECT '、'+MS FROM #lsb where SYXH=t.SYXH FOR XML PATH('')),1,1,'') AS MS
                        FROM #lsb as t 670個

 

 

 

https://www.cnblogs.com/lcs-java/articles/8465605.htmlhtml

 

t1表內容以下:java

t2表內容以下:sql

下面來簡述join和left join/right join的區別:post

inner join3d

select * from t1 inner join t2 on t1.id = t2.id;htm

公共部分的數據纔會被查詢出來;blog

left joinget

select * from t1 left join t2 on t1.id = t2.id;it

查詢出來的結果和前表記錄數同樣多;class

right join

select * from t1 right join t2 on t1.id = t2.id;

能轉化爲

相關文章
相關標籤/搜索