user表:
id | name --------- 1 | libk 2 | zyfon 3 | daodaouser_action表:
user_id | action --------------- 1 | jump 1 | kick 1 | jump 2 | run 4 | swim
sql:select id, name, action from user as uleft 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 是一个表满足条件的行,和另一个表的所有行。
转载于:https://www.cnblogs.com/isoftware/p/3763736.html