- SELECT a.uid,a.uname,a.joindate,b.chargedate,b.amount
- FROM useraccount a
- LEFT JOIN chargehistory b
- ON a.uid=b.uid AND a.uname='mike'
无论是 left join 还是 right join 的时候 on 后面的只允许是两个表中的关联字段,多个的话使用and连接,如果你写了某一个表的条件在里面,就像上面的例子,其实a.uname='mike'是不起作用的。所以在使用 left join 和 right join 的时候需要注意,如果不是两个表的关联字段都需要写在where语句的后面。
正确写法:
- SELECT a.uid,a.uname,a.joindate,b.chargedate,b.amount
- FROM useraccount a
- LEFT JOIN chargehistory b
- ON a.uid=b.uid
- WHERE a.uname='mike'
转载请注明:观测者 » left join on ...and ... 与left join on ... where.. 的区别