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.html

 

t1表内容如下:

t2表内容如下:

下面来简述join和left join/right join的区别:

inner join

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

公共部分的数据才会被查询出来;

left join

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

查询出来的结果和前表记录数一样多;

right join

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

能转化为

 

posted @ 2018-07-26 10:41  彼岸xy  阅读(32817)  评论(0编辑  收藏  举报