又叫多表连接,当查询结果来自多张表时,需要将多张表连接成一个大的数据集汇总显示
select 字段 from 表1 连接类型 join 表2 on 连接条件 where 筛选条件;
分类:
笛卡尔集
select * from student inner join class;
没有有效的连接条件
基础语法
等值连接
select s.name,c.name from student s,class c,teacher t where s.classid=c.id,c.id=t.cid;
非等值连接
select s.name,s.score,l.name from student s,score_level l where s.score between l.min_score and l.max_score order by s.score desc;
自连接
select s.id,s.name,leader.id,leader.name from student s,student leader where s.leader_id=leader.id;
select s.id,s.name,c.name from student s inner join class c where s.classid=c.id;
select l.name,count(*) from student s inner join score_level l on s.score between l.min_score and l.max_score group by l.name having count(*)>3 order by l.name asc;
select s.id,s.name,leader.id,leader.name from student s inner join student leader on s.leaderid=leader.id;
select sel_list from A left join B on A.bid=B.id where B.id is null
select sel_list from A right join B on A.id=B.aid where A.id is null
select sel_list from A full join B on A.key=B.key where A.key is null and B.key is null