多表查询
连接查询:
交叉连接:
1.使用cross join关键字
2.不使用
内连接:inner join(inner可省)
---返回(返回匹配上的数据,匹配不上的就不要了)两个表的交集
显式内连接:
select*from 表1 inner join 表2 on 关联条件
select*from classes c join student s on c.cid=s.cno;
▲隐式内连接:
select*from表1,表2 where 关联条件 ---业务需求
select*from classes c,student s where c.cid=s.cno and s.sex='男';
外连接:outer join(outer可省)
---以左表为基准,根据关联条件去右表查询,匹配上的则返回匹配数据,匹配不上返回null
▲左外连接:(outer可省)
select*from 表1 left outer join 表2 on 关联条件
select*from classes c left join student s on c.cid=s.cno;
右外连接:
select*from 表1 right outer join 表2 on 关联条件
select*from classes c right join student s on c.cid=s.cno;
子查询:
带in的子查询: ---在条件范围内
select*from classes where cid in (select cno from student where birthday>'1991-01-01');
带exists的子查询:---if条件判断,子句为true,执行前边
select*from classes where exists (select cno from student where birthday>'1991-01-01');
带any的子查询: ---大于任意的就是大于最小值
select * from classes where cid >any (select cno from student);
带all的子查询: ---大于所有的就是大于最大值
select*from classes where cid > all (select cno from student);