首页 > 代码库 > sql连接总结

sql连接总结

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;

sql连接总结