user表:
user_action表:
sql:
select id, name, action from user as u
left join
user_action a on u.id = a.user_id
result:
id |
name |
action
--------------------------------
1
| libk |
jump ①
1 |
libk | kick
②
1 | libk
| jump ③
2
| zyfon | run
④
3 | daodao |
null ⑤
sql例子:
select employee.*, department.name as deptname
from
employee
left join department on employee.department =
department.code
where employee.isuse = ‘1‘
and employee.code
= ‘3700000000‘
我们可以想象left join
是这样工作的
从左表读出一条,选出所有与on匹配的右表纪录(n条)进行连接,
形成n条纪录(包括重复的行,如:结果1和结果3),
如果右边没有与on条件匹配的表,那连接的字段都是null.
然后继续读下一条。
LEFT JOIN 等价于LEFT OUTER JOIN 是一个表满足条件的行,和另一个表的所有行。
原文地址:http://www.cnblogs.com/isoftware/p/3763736.html