您的位置:首页 > 博客中心 > 数据库 >

MSSQLSERVER数据库- LEFT JOIN后面跟着WHERE竟变成内联 解决方法

时间:2022-03-14 03:02

  在LEFT JOIN后面跟着WHERE竟变成内联。百度后看到这个解决方法。

  记录如下:

 

 select sre.*, co.description 
 from subscribedratingelement sre left outer join custom_options co on sre.locationInCdr=co.optionvalue 
 where co.optionname=‘LocationInCdr‘;

 

 select sre.*, co.description 
 from subscribedratingelement sre left outer join custom_options co 
 on (sre.locationInCdr=co.optionvalue and co.optionname=‘LocationInCdr‘);

 

第一条SQL是一个左外连接,然后进行where过滤。仔细分析这个SQL会发现,最后的结果不是所期望的,custom_options表中不符合条件的记录本来是以null表示的,由于where中的过滤,导致查询出来的记录为null的部分都没有查询出来。这个左外连接就和内连接没有任何区别了。

第二个SQL语句就可以满足要求。做连接的时候就过滤了右边的一些记录,这样就算右表不符合条件的左表记录也可以查询出来。

 

热门排行

今日推荐

热门手游