多表查询的分类?
笛卡尔积查询(最简单的多表查询,是建立在一切多表查询基础之上的)
select * from a,b;
内连接查询
隐式内连接查询
select * from a, b where a.id = b.id;
显示内连接查询
select * from a inner join b on a.id = b.id;
说明:不管是显示还是隐式,结果一模一样,用哪个,看你心情
外连接查询
左外连接查询
select * from a left join b on a.id = b.id;
select * from b left join a on a.id = b.id;
右外连接查询
select * from a right join b on a.id = b.id;
select * from b right join a on a.id = b.id;
子查询
select name from a where id = (select id from b where name = '李四');
子查询可以对表进行重构
--需求:查询a表中的所有信息,id值降序排列(单表查询)
select * from a order by id desc;
--需求:查询a表中的所有信息,id值降序排列(子查询)
select newA.* from (select * from a order by id desc) newA;