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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。