database left join

来源:互联网 发布:魂乎少年用日语怎么说 编辑:程序博客网 时间:2024/06/05 17:44

在不同的位置,得到的结果可能会不同,这一点要注意。

两个表关联条件写在on后面,限制条件写在where后面,这个是错误的。

正确的是:

进行左连接时,就有涉及到主表、辅表,这时主表条件写在WHERE之后,辅表条件写在ON后面!!!

select a.* from a left join b on a.id=b.id andb.flag=0 where a.flag=0

 

以下写法都是不对的。

1、selecta.* from a left join b on a.id=b.id where b.flag=0 anda.flag=0

测试少记录

2、select a.* from a left join b ona.id=b.id and b.flag=0 and a.flag=0

测试多记录

3、select a.* from a left join b on a.id=b.id and a.flag=0 whereb.flag=0

测试少记录

4、select a.* from a left join b on a.id=b.id where (b.flag=0 orb.flag is null) and a.flag=0

测试少记录


原创粉丝点击