您好,登錄后才能下訂單哦!
1.內連接inner join
1.1交叉連接cross join
SELECT * FROM person_hobby,person,hobby;
表A(m列),表B(n列),表C(l列)的笛卡爾積(m*n*l列)
1.2等值連接
SELECT * FROM person as p,hobby as h,person_hobby as ph where p.person_id=ph.person_id and h.hobby_id=ph.hobby_id;
在交叉連接的基礎上根據條件進行選擇(過濾掉不合法條件的行)
1.3自然連接natural join
SELECT * FROM person natural join person_hobby natural join hobby;
在等值連接的基礎上去除重復列(若A,B有相同名稱的列,否則就等價于交叉連接)
2.外連接outter join
2.1左外連接left join
SELECT * FROM person_hobby right join person on person_hobby.person_id=person.person_id left join hobby on person_hobby.hobby_id=hobby.hobby_id;
左表全部行和右表對應左表的行
2.2右外連接right join
SELECT * FROM person_hobby right join person on person_hobby.person_id=person.person_id right join hobby on person_hobby.hobby_id=hobby.hobby_id;
右表的全部行和左表對應右表的行
2.3全外連接full join(部分數據庫管理系統支持,mysql不支持)
select * from A full join B on A.a=B.a;
3.聯合union:將多個查詢結果合并,去除重復行(union all包含重復行)
select ... from A where ... union select ...from b where ... union select ... from c where ... group by ... order by ...
使用規則:
每個select語句需要選取相同數量的,數據類型兼容的列。
group by或order by只能在最后使用一次,不能每個select都使用,因為union是合并之后再進行分組或排序。
4.intersect:求交集
select ... from A where ... intersect select ...from b where ... intersect select ... from c where ...
5.except:求差集
select ... from A where ... except select ...from b where ... except select ... from c where ...
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。