mysql 多表查询

MySQL multi-table query

Posted by Lv Hui on June 24, 2014

笛卡尔积

1
select * from stu,team;

表关联

笛卡尔积表关联

通过在笛卡尔积后面加上限制条件来达到关联效果

1
select * from stu,team where stu.uid=team.uid;

内关联

1
2
3
select * from stu inner join team on stu.uid=team.uid;
//可以不写inner
select * from stu join team on stu.uid=team.uid;

三张表关联

1
select * from stu join middle on stu.uid=middle.uid join team on middle.sid=team.sid;

左关联与右关联

以一侧的表为主,即显示该表的所有数据,即使没有所关联的数据

1
2
select * from stu left join team on stu.uid=team.uid;
select * from stu right join team on stu.uid=team.uid;

自关联

自关联时必须起别名

1
select * from stu as s1 join stu as s2 on s1.uid=s2.uid where s1.name='liming';