<!--
1.自然连接(natural)
自然连接是用来简化'内连接和外连接'的
如果多张表需要判断的条件字段名称一致, 那么不用编写条件, 自然连接会自动判断
1.1自然内连接
select * from 表名1 inner join 表名2 on 条件;
select * from stu inner join grade on stu.id = grade.stuId;
select * from 表名1 natural join 表名2;
select * from stu natural join grade;
1.2自然外连接
1.2.1自然左外连接
select * from stu natural left join grade;
1.2.1自然右外连接
select * from stu natural right join grade;
注意点:
- 如果没有指定条件, 也没有同名的字段, 那么就会返回笛卡尔集
- 在自然连接中, 返回的结果集会自动优化, 会自动去除重复的判断字段
-->
<!--
1.using关键字
如果多张表需要判断的条件字段名称一致, 那么除了可以使用自然连接来简化以外
还可以使用using关键字来简化
1.1内连接
select * from stu inner join grade on stu.stuId = grade.stuId;
select * from stu inner join grade using(stuId);
1.2外连接
1.2.1左外连接
select * from stu left join grade on stu.stuId = grade.stuId;
select * from stu left join grade using(stuId);
1.2.2右外连接
select * from stu right join grade on stu.stuId = grade.stuId;
select * from stu right join grade using(stuId);
-->
<!--
1.子查询
- 将一个查询语句查询的结果作为另一个查询语句的条件来使用
- 将一个查询语句查询的结果作为另一个查询语句的表来使用
2.将一个查询语句查询的结果作为另一个查询的条件来使用
2.1标准子查询(返回的结果只有一个)
select stuId from grade where score = 100;
select name from stu where stuId = 3;
select name from stu where stuId = (select stuId from grade where score = 100);
2.2非标准子查询(返回的结果有多个)
select stuId from grade where score >= 60;
select name from stu where stuId = 3 OR stuId = 1;
select name from stu where stuId in(3, 1);
select name from stu where stuId in(select stuId from grade where score >= 60);
3.将一个查询语句查询的结果作为另一个查询的表来使用
select name, city, score from person where score >= 60;
select name, city, score from (select name, city, score from person where score >= 60) as t;
注意点:
如果要将一个查询语句查询的结果作为另一个查询的表来使用, 那么必须给子查询起一个别名
-->