MySQL多表连接查询详解
16lz
2021-12-10
多表连接查询
表与表之间的连接分为内连接和外连接
- 内连接:仅选出两张表互相匹配的记录
- 外连接:既包括两张表匹配的记录,也包括不匹配的记录,同时外连接又分为左外连接(左连接)和右外连接(右连接)
内连接
首先准备两张表
学生student表
分数score表
内连接:在每个表中找出符合条件的共有记录
查询student表中的学生姓名和分数
第一种写法:只使用where
select a.s_name, b.s_score from student a,score b where a.s_id = b.s_id;
select a.s_name, b.s_score from student a join score b on a.s_id = b.s_id
select a.s_name, b.s_score from student a inner join score b on a.s_id = b.s_id
第一种写法:left join … on …
select a.s_name,b.s_score from student a left join score b on a.s_id = b.s_id
select a.s_name,b.s_score from student a left outer join score b on a.s_id = b.s_id
第一种写法:right join … on …
select a.s_name,b.s_score from student a right join score b on a.s_id = b.s_id;
select a.s_name,b.s_score from student a right outer join score b on a.s_id = b.s_id;
WHERE子句中的子查询:子查询返回的值作为主查询的查询条件
FROM子句中的子查询:子查询返回的是一张虚拟的表,主查询从该查询从临时表查询出满足的条件
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!
更多相关文章
- MySQL系列多表连接查询92及99语法示例详解教程
- Linux下MYSQL 5.7 找回root密码的问题(亲测可用)
- MySQL 什么时候使用INNER JOIN 或 LEFT JOIN
- Android(安卓)- Manifest 文件 详解
- Android的Handler机制详解3_Looper.looper()不会卡死主线程
- Selector、shape详解(一)
- android2.2资源文件详解4--menu文件夹下的菜单定义
- android从服务器下载文件(php+apache+win7+MySql)
- Android发送短信方法实例详解