表結構見前面博客
1.內連接
1.1.等值連接:在連接條件中使用等於號(=)運算符比較被連接列的列值,其查詢結果中列出被連接表中的所有列,包括其中的重復列。
三種寫法:
select * from t_fn_person a , t_fn_dept b where a.dept_id=b.dept_id; select * from t_fn_person a join t_fn_dept b on a.dept_id=b.dept_id; select * from t_fn_person a inner join t_fn_dept b on a.dept_id=b.dept_id;
查詢結果:
1.2.不等值連接:在連接條件使用除等於運算符以外的其它比較運算符比較被連接的列的列值。這些運算符包括>、>=、<=、<、!>、!<和<>。
1.3.自然連接:在連接條件中使用等於(=)運算符比較被連接列的列值,但它使用選擇列表指出查詢結果集合中所包括的列,並刪除連接表中的重復列。
注:
內連接:內連接查詢操作列出與連接條件匹配的數據行,它使用比較運算符比較被連接列的列值。
自然連接一定是等值連接,但等值連接不一定是自然連接。
等值連接要求相等的分量,不一定是公共屬性;而自然連接要求相等的分量必須是公共屬性。
等值連接不把重復的屬性除去;而自然連接要把重復的屬性除去。
2.外連接
2.1.左聯接:是以左表為基准,將a.dept_id=b.dept_id的數據進行連接,然后將左表沒有的對應項顯示,右表的列為NULL
三種寫法:
select * from t_fn_person a left join t_fn_dept b on a.dept_id=b.dept_id; select * from t_fn_person a left outer join t_fn_dept b on a.dept_id=b.dept_id; select * from t_fn_person a , t_fn_dept b where a.dept_id=b.dept_id(+);
查詢結果:
2.2.右連接:是以右表為基准,將a.dept_id=b.dept_id的數據進行連接,然以將右表沒有的對應項顯示,左表的列為NULL
三種寫法:
select * from t_fn_person a right join t_fn_dept b on a.dept_id=b.dept_id; select * from t_fn_person a right outer join t_fn_dept b on a.dept_id=b.dept_id; select * from t_fn_person a , t_fn_dept b where a.dept_id(+)=b.dept_id;
查詢結果:
2.3.全連接:完整外部聯接返回左表和右表中的所有行。當某行在另一個表中沒有匹配行時,則另一個表的選擇列表列包含空值。如果表之間有匹配行,則整個結果集行包含基表的數據值。
三種寫法:
select * from t_fn_person a full join t_fn_dept b on a.dept_id=b.dept_id; select * from t_fn_person a full outer join t_fn_dept b on a.dept_id=b.dept_id;
查詢結果: