江哥MySQL视频学习笔记--第07章 MySQL高级查询-48 49 50-MySQL-多表查询(掌握)

<!--

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;

注意点:

如果要将一个查询语句查询的结果作为另一个查询的表来使用, 那么必须给子查询起一个别名

-->

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值