【MySQL复习】嵌套查询&内连接查询&外连接查询


一、嵌套连接

select * from user where cid = (select id from class where cName='1班';

查询1班的学生信息

二、内连接

select * from user inner join class where user.cid = class.id and class.cName = '1班';

同理:

select * from user,class where user.cid = class.id and class.cName = '1班';

也可以起别名

select s.name,s.id,c.cName from user as s,class as c where s.cid = c.id and c.cName = '1班';

三、外连接

左连接:左表的所有数据和右表满足条件的数据

select * from student as s left join class as c where s.cid = c.id and s.id = 1;

 右链接:右表的所有数据和左表满足条件的数据

注:一般使用内连接进行查询,外连接在特殊情况使用!