employee表
department表
1.笛卡爾積
select * from employee a ,department b;
共4*2=8條結果。
2.inner join
select * from employee a
inner join department b
on a.DepartmentId=b.id;
等價於
select * from employee a ,department b
where a.DepartmentId=b.id;
3.left join
返回滿足連接條件的左邊表的所有記錄,若左邊表的某些記錄在右邊表中沒有匹配記錄,右邊表則顯示null。
(1)如果不指定連接條件,left join的結果將和笛卡爾積的結果相同。
select * from employee a
left join department b;
(2)employee left join department
select * from employee a
left join department b
on a.DepartmentId=b.id;
(3)department left join employee
交換一下left join左右兩表的位置:
department left join employee的結果 = employee right join department的結果(顯示位置不同)
select * from department a
left join employee b
on a.id=b.departmentId;
4.right join
返回滿足連接條件的右邊表的所有記錄,若右邊表的記錄在左邊表中沒有匹配記錄,左邊表則顯示null。
select * from employee a
right join department b;
select * from employee a
right join department b
on a.departmentId=b.id;
等價於
select * from employee a
left join department b
on a.departmentId=b.id
where b.name is not null;