【MySQL】MySQL内连接,左连接,右连接查询
概念
- INNER JOIN(内连接):获取两个表中字段匹配关系的记录。也就是只会返回共有的内容。
- LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
- RIGHT JOIN(右连接): 获取右表所有记录,即使左表没有对应匹配的记录。
示例
先在数据库中建立两张表student和score,具体内容如下:
【student】
mysql> select * from student;
--------------
select * from student
-------------- +----+---------------------+------+-------+------------+-----------+
| id | name | sex | birth | department | address |
+----+---------------------+------+-------+------------+-----------+
| 1 | RooneyMara | F | 1985 | Psychology | American |
| 2 | ChrisHemsworth | M | 1983 | CS | Australia |
| 3 | EllenPage | F | 1987 | Music | Canada |
| 4 | TomHolland | M | 1996 | CS | England |
| 5 | ScarlettJohansson | F | 1984 | Music | American |
| 6 | BenedictCumberbatch | M | 1976 | Psychology | England |
| 7 | EvaGreen | F | 1980 | Math | France |
+----+---------------------+------+-------+------------+-----------+
7 rows in set (0.00 sec)
【score】
mysql> select * from score;
--------------
select * from score
-------------- +----+--------+------------+-------+
| id | stu_id | c_name | grade |
+----+--------+------------+-------+
| 1 | 1 | Psychology | 98 |
| 2 | 1 | Music | 80 |
| 3 | 2 | Psychology | 65 |
| 4 | 2 | CS | 88 |
| 5 | 3 | CS | 95 |
| 6 | 4 | Psychology | 70 |
| 7 | 4 | Music | 92 |
| 8 | 5 | Music | 94 |
| 9 | 6 | Psychology | 90 |
| 10 | 6 | CS | 85 |
| 11 | 8 | Music | 91 |
+----+--------+------------+-------+
11 rows in set (0.00 sec)
内连接
查询student表中的所有个人信息及score表中的c_name,grade
mysql> select a.*,c_name,grade from student a join score b on a.id=b.stu_id;
--------------
select a.*,c_name,grade from student a join score b on a.id=b.stu_id
-------------- +----+---------------------+------+-------+------------+-----------+------------+-------+
| id | name | sex | birth | department | address | c_name | grade |
+----+---------------------+------+-------+------------+-----------+------------+-------+
| 1 | RooneyMara | F | 1985 | Psychology | American | Psychology | 98 |
| 1 | RooneyMara | F | 1985 | Psychology | American | Music | 80 |
| 2 | ChrisHemsworth | M | 1983 | CS | Australia | Psychology | 65 |
| 2 | ChrisHemsworth | M | 1983 | CS | Australia | CS | 88 |
| 3 | EllenPage | F | 1987 | Music | Canada | CS | 95 |
| 4 | TomHolland | M | 1996 | CS | England | Psychology | 70 |
| 4 | TomHolland | M | 1996 | CS | England | Music | 92 |
| 5 | ScarlettJohansson | F | 1984 | Music | American | Music | 94 |
| 6 | BenedictCumberbatch | M | 1976 | Psychology | England | Psychology | 90 |
| 6 | BenedictCumberbatch | M | 1976 | Psychology | England | CS | 85 |
+----+---------------------+------+-------+------------+-----------+------------+-------+
10 rows in set (0.00 sec)
以上语句等价于:
mysql> select a.*,c_name,grade from student a,score b where a.id=b.stu_id;
--------------
select a.*,c_name,grade from student a,score b where a.id=b.stu_id
-------------- +----+---------------------+------+-------+------------+-----------+------------+-------+
| id | name | sex | birth | department | address | c_name | grade |
+----+---------------------+------+-------+------------+-----------+------------+-------+
| 1 | RooneyMara | F | 1985 | Psychology | American | Psychology | 98 |
| 1 | RooneyMara | F | 1985 | Psychology | American | Music | 80 |
| 2 | ChrisHemsworth | M | 1983 | CS | Australia | Psychology | 65 |
| 2 | ChrisHemsworth | M | 1983 | CS | Australia | CS | 88 |
| 3 | EllenPage | F | 1987 | Music | Canada | CS | 95 |
| 4 | TomHolland | M | 1996 | CS | England | Psychology | 70 |
| 4 | TomHolland | M | 1996 | CS | England | Music | 92 |
| 5 | ScarlettJohansson | F | 1984 | Music | American | Music | 94 |
| 6 | BenedictCumberbatch | M | 1976 | Psychology | England | Psychology | 90 |
| 6 | BenedictCumberbatch | M | 1976 | Psychology | England | CS | 85 |
+----+---------------------+------+-------+------------+-----------+------------+-------+
10 rows in set (0.00 sec)
左连接
student表中id为7的数据,在score中没有对应的内容。所以最后一条查询结果c_name,grade对应内容为null。
mysql> select a.*,c_name,grade from student a left join score b on a.id=b.stu_id;
--------------
select a.*,c_name,grade from student a left join score b on a.id=b.stu_id
-------------- +----+---------------------+------+-------+------------+-----------+------------+-------+
| id | name | sex | birth | department | address | c_name | grade |
+----+---------------------+------+-------+------------+-----------+------------+-------+
| 1 | RooneyMara | F | 1985 | Psychology | American | Psychology | 98 |
| 1 | RooneyMara | F | 1985 | Psychology | American | Music | 80 |
| 2 | ChrisHemsworth | M | 1983 | CS | Australia | Psychology | 65 |
| 2 | ChrisHemsworth | M | 1983 | CS | Australia | CS | 88 |
| 3 | EllenPage | F | 1987 | Music | Canada | CS | 95 |
| 4 | TomHolland | M | 1996 | CS | England | Psychology | 70 |
| 4 | TomHolland | M | 1996 | CS | England | Music | 92 |
| 5 | ScarlettJohansson | F | 1984 | Music | American | Music | 94 |
| 6 | BenedictCumberbatch | M | 1976 | Psychology | England | Psychology | 90 |
| 6 | BenedictCumberbatch | M | 1976 | Psychology | England | CS | 85 |
| 7 | EvaGreen | F | 1980 | Math | France | NULL | NULL |
+----+---------------------+------+-------+------------+-----------+------------+-------+
11 rows in set (0.00 sec)
右连接
score表中id为11的数据,在student中没有对应的内容,所以最后一条查询结果id,name,sex等对应内容为null。
mysql> select a.*,c_name,grade from student a right join score b on a.id=b.stu_id;
--------------
select a.*,c_name,grade from student a right join score b on a.id=b.stu_id
-------------- +------+---------------------+------+-------+------------+-----------+------------+-------+
| id | name | sex | birth | department | address | c_name | grade |
+------+---------------------+------+-------+------------+-----------+------------+-------+
| 1 | RooneyMara | F | 1985 | Psychology | American | Psychology | 98 |
| 1 | RooneyMara | F | 1985 | Psychology | American | Music | 80 |
| 2 | ChrisHemsworth | M | 1983 | CS | Australia | Psychology | 65 |
| 2 | ChrisHemsworth | M | 1983 | CS | Australia | CS | 88 |
| 3 | EllenPage | F | 1987 | Music | Canada | CS | 95 |
| 4 | TomHolland | M | 1996 | CS | England | Psychology | 70 |
| 4 | TomHolland | M | 1996 | CS | England | Music | 92 |
| 5 | ScarlettJohansson | F | 1984 | Music | American | Music | 94 |
| 6 | BenedictCumberbatch | M | 1976 | Psychology | England | Psychology | 90 |
| 6 | BenedictCumberbatch | M | 1976 | Psychology | England | CS | 85 |
| NULL | NULL | NULL | NULL | NULL | NULL | Music | 91 |
+------+---------------------+------+-------+------------+-----------+------------+-------+
11 rows in set (0.00 sec)
【MySQL】MySQL内连接,左连接,右连接查询的更多相关文章
- Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符)
Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符) 一丶多表查询 多表连接查询的应用场景: 连接是关系数据库模型的主要特点,也是区别于其他 ...
- SQL-内连接、外连接(左、右)、交叉连接
本文测试基于以下两个表,student(左) \ teacher(右),使用数据库MariaDB,图形化界面HeidiSQL. 连接查询的概念:根据两个表或多个表的列之间的关系,从这些表中查询数据,即 ...
- LINQ 内链接 左链接 右链接
原文地址:http://blog.sina.com.cn/s/blog_46e9573c01014fx2.html 1.左连接: var LeftJoin = from emp in ListOfEm ...
- 分享知识-快乐自己:MYSQL之內链接 左链接 右链接 区别
MYSQL中可以通过内外键链接,将有关系的表中数据合并到一起进行条件筛选: 首先创建两个新表,数据如下: student 表数据: score 表数据: 可以看到students表中stu_id为16 ...
- mysql 内连接 左连接 右连接 外连接
mysql> desc student;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | ...
- 图解MySQL 内连接、外连接、左连接、右连接、全连接
用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...
- MySQL中的内连接、左连接、右连接、全连接、交叉连接
创建两个表(a_table.b_table),两个表的关联字段分别为:a_table.a_id和b_table.b_id CREATE TABLE a_table ( a_id int NOT NUL ...
- MySQL 内连接、外连接、左连接、右连接、全连接……太多了
用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). 主题:内连接 ...
- mysql内连接(inner join 找两个表的交集)、左连接(left join 交集并且左表所有)、右连接(right join 交集并且右表所有)、全连接(mysql不支持)
用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...
- MySQL之左连接与右连接
左连接: select 列1,列2,列N from tableA left join tableB on tableA.列 = tableB.列(正常是一个外键列) [此处表连接成一张大表,完全当成一 ...
随机推荐
- [转帖]万字详解Oracle架构、原理、进程,学会世间再无复杂架构
万字详解Oracle架构.原理.进程,学会世间再无复杂架构 http://www.itpub.net/2019/04/24/1694/ 里面的图特别好 数据和云 2019-04-24 09:11:59 ...
- centos 7 修改网卡名字
1.编辑网卡信息 cd /etc/sysconfig/network-scripts/ #进入网卡目录mv ifcfg-en067761 ifcfg-eth0 #重命名网卡 cat ifcfg-eth ...
- Springboot读取Jar文件中的resource
如题,碰到了问题. 事情是这样的. 一个导入模板, 因为比较少, 所以就直接放在后台的resources中了.调试的时候是下载没有问题的. 等到发布后,下载就出问题了. 参照: ***.jar!\BO ...
- day02-运算符 and 和 or 的用法
# 运算符的优先级 # () > not > and > or # and 必须两边表达式都为真,才为真 # or 如果表达式有一边为真,则为真 # not 表示非.比如 not 2 ...
- Async/Await 学习与示例
参考:Async/await学习 es 7 提供了对 promise 对象的更好的操作,省去了很多丧心病狂的链式异步请求,promise 是回调地狱的福音,而 Async/Await 则是 promi ...
- Leetcode 4.28 Tree Easy
1. 101. Symmetric Tree 用递归. class Solution { public boolean isSymmetric(TreeNode root) { if( root == ...
- vscode的插件收集
转:https://zhuanlan.zhihu.com/p/27905838 转:https://segmentfault.com/a/1190000006697219
- MVN TEST指定运行脚本
clean:表示将你上一次编译生成的一些文件删除 test:表示只执行测试代码 >mvn clean test -Dtest=[ClassName] 运行测试类中指定的方法:这个需要maven- ...
- JavaScript中数据类型判断
做判断前先来了解下 JavaScript 中的基本数据类型 一.七大数据类型 基本类型:存储在栈( stack )中 Number(包括整型和浮点型) String. Boolean. Symbol ...
- mysq建表参数设置
建表的完整性约束: not null 与 default unique primary auto_increment foreign key 外键的变种 三种关系 一.介绍 约束条件与数据类型的宽度 ...