user表:html
user_action表:sql
sql:
select id, name, action from user as u
left join user_action a on u.id = a.user_idide
result:
id | name | action
--------------------------------
1 | libk | jump ①
1 | libk | kick ②
1 | libk | jump ③
2 | zyfon | run ④
3 | daodao | null ⑤spa
sql例子:code
select employee.*, department.name as deptname
from employee
left join department on employee.department = department.code
where employee.isuse = '1'
and employee.code = '3700000000'orm
咱們能夠想象left join 是這樣工做的
從左表讀出一條,選出全部與on匹配的右表紀錄(n條)進行鏈接, 造成n條紀錄(包括重複的行,如:結果1和結果3),
若是右邊沒有與on條件匹配的表,那鏈接的字段都是null.
而後繼續讀下一條。htm
LEFT JOIN 等價於LEFT OUTER JOIN 是一個表知足條件的行,和另外一個表的全部行。blog