【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.列(正常是一个外键列) [此处表连接成一张大表,完全当成一 ...
随机推荐
- JS中让新手倍感震惊、违反直觉、出乎意料、的一些知识点汇总记录
本文记录在自己学习js过程中,违反直觉,出乎意料,倍感震惊的知识点.当然,不了解这个知识点,很容易出错,因为毕竟违反直觉,出乎意料,倍感震惊嘛! 1. 两个内容一样的数组竟然不相等? var a = ...
- eclipse中查看字节码
1:在线安装ByteCode插件 打开Eclipse Go to"Help -> Install new Software... -> Work with:"中选择By ...
- 01-jQuery基本语法
一.jQuery基础 1.为什么要用jquery? 写起来简单,省事,开发效率高,兼容性好2.什么是jQuery? jQuery是一个兼容多浏览器的JavaScript库(类似python里面的模块) ...
- delphi中响应鼠标进入或离开控件的方法
Delphi没有MouseEnter与MouseLeave的事件,网上说可以响应CM_MOUSEENTER和CM_MOUSELEAVE消息来实现.这两个消息是VCL自己定义的消息,看了Delphi的C ...
- Go语言中数组
数组是一个值类型 func ArrayTest1(){ var arryA [3]int = [3]int{1,2,3} //创建一个数组B,将B中第二个元素设置为200 arryB := arryA ...
- jatoolsprinter html实现每隔几秒获取数据直接后台打印不弹窗
1.流程说明 jatoolspringter 必须要能在html代码里面看到 id =page1 page2 page3..... 才能打印,所以无法动态打印,必须先把要打印的内容放到页面某个地方隐 ...
- Gym - 101982B Coprime Integers (莫比乌斯反演)
题目链接:http://codeforces.com/gym/101982/attachments 题目大意:有区间[a,b]和区间[c,d],求gcd(x,y)=1,其中x属于[a,b],y属于[c ...
- 互联网+ 何人能挡?带着你的Code飞奔吧!
Python方向: 早期方向 Web全栈 擅长专栏 爬虫系列 数据分析 人工智能 物联网系(lot万物互联)[逆天很看好未来的前景] 自动化运维(安全与测试) 其他系列 游戏开发(最近很火) 导航栏: ...
- 史上最全PMP备考考点全攻略(上篇-五大过程组,附赠资料)
一.这可能是一篇史上最全的PMP备考考点全梳理文章 写在前面,这可能是史上最全的PMBOK考点全书考点梳理,由PMP备考自律营呕心沥血整理,内容较长,分为上下篇,绝对值得所有正在备考PMP的学员收藏! ...
- Qt: 执行cmd命令;
QProcess p(NULL); p.setWorkingDirectory(szAppPath+"/database"); //指定工作路径,这个地方一定要设置: p.star ...