1) select * from 表1 left join 表2 on 表1=表2 where 表1.id>10 ;
//不考虑表2 的重复值有40条数据
返回 表1 的所有数据40条。如果表2中没有对应的值以null 返回。
1.1) select * from 表1 right join 表2 on 表1=表2 where 表1.id>10 ;
这时主表是 表2 ,如果表1中没有对应的值会以 null 返回,并且会被where条件过滤掉;
//不考虑表2 的重复值有40条数据
返回 表2 的数据30条。如果表1中没有对应的值会被过滤(10条)。
2) select * from 表1 left join 表2 on 表1=表2
where 表1.id>10 and 表2.time>unix_timestamp('2017-09-01');
//不考虑表2 的重复值有20条数据
返回 表1 的20条数据。如果表2中没有对应的值null则会被过滤调。
3) select * from 表1 left join 表2 on 表1=表2 and 表2.time>unix_timestamp('2017-09-01')
where 表1.id>10 ;
//不考虑表2 的重复值有30条数据(10条是表2的null值)
返回 表1 的30条数据。如果表2中没有对应的值以null 返回。
总结:外连接查询如果非主表有条件,必须加在 :left Join .. on .. and (条件) ; 这样才不会过滤掉表2 的null值(有空再补图)