概念

  • 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内连接,左连接,右连接查询的更多相关文章

  1. Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符)

    Python进阶----多表查询(内连,左连,右连), 子查询(in,带比较运算符) 一丶多表查询     多表连接查询的应用场景: ​         连接是关系数据库模型的主要特点,也是区别于其他 ...

  2. SQL-内连接、外连接(左、右)、交叉连接

    本文测试基于以下两个表,student(左) \ teacher(右),使用数据库MariaDB,图形化界面HeidiSQL. 连接查询的概念:根据两个表或多个表的列之间的关系,从这些表中查询数据,即 ...

  3. LINQ 内链接 左链接 右链接

    原文地址:http://blog.sina.com.cn/s/blog_46e9573c01014fx2.html 1.左连接: var LeftJoin = from emp in ListOfEm ...

  4. 分享知识-快乐自己:MYSQL之內链接 左链接 右链接 区别

    MYSQL中可以通过内外键链接,将有关系的表中数据合并到一起进行条件筛选: 首先创建两个新表,数据如下: student 表数据: score 表数据: 可以看到students表中stu_id为16 ...

  5. mysql 内连接 左连接 右连接 外连接

    mysql> desc student;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | ...

  6. 图解MySQL 内连接、外连接、左连接、右连接、全连接

    用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...

  7. MySQL中的内连接、左连接、右连接、全连接、交叉连接

    创建两个表(a_table.b_table),两个表的关联字段分别为:a_table.a_id和b_table.b_id CREATE TABLE a_table ( a_id int NOT NUL ...

  8. MySQL 内连接、外连接、左连接、右连接、全连接……太多了

    用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). 主题:内连接 ...

  9. mysql内连接(inner join 找两个表的交集)、左连接(left join 交集并且左表所有)、右连接(right join 交集并且右表所有)、全连接(mysql不支持)

    用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...

  10. MySQL之左连接与右连接

    左连接: select 列1,列2,列N from tableA left join tableB on tableA.列 = tableB.列(正常是一个外键列) [此处表连接成一张大表,完全当成一 ...

随机推荐

  1. Tutorial 03_分布式数据库HBASE

    (一)编程实现一下内容,并用Hadoop提供的Shell命令完成相同任务: 编程实现: (1)列出HBase所有表的相关信息,例如表名; package tutorial01; import java ...

  2. mysql联合主键,也就是两个数据字段一起做主键的情况

    一个数据表,需要两个字段联合起来一块做主键的时候.举例如下: 直接用sql语句的话如下 ALTER TABLE `表名` ADD PRIMARY KEY ( `表中字段名1` , `表中字段名2` ) ...

  3. SpringCloud-Ribbon服务调用(三)

    SpringCloud-Ribbon服务调用(三) https://www.cnblogs.com/qdhxhz/p/9568481.html 一.商品中心服务端 创建商品中心服务端:服务提供方(被调 ...

  4. springboot 的部分细节

    Application.properties 中#指定端口号 server.port= #指定访问路径必须以/crud/xxx 开始 server.servlet.context-path=/crud ...

  5. nginx 返回json格式内容

    例子: #如果访问的ip是192.168.1.1,就直接返回json格式的内容 location / { default_type application/json; #####格式 if ( $re ...

  6. JS继承以及继承的几种实现方式总结

    传统面向对象语言:继承是类与类之间的关系. 而在js中由于es6之前没有类的概念,所以继承是对象与对象之间的关系. 在js中,继承就是指使一个对象有权去访问另一个对象的能力. 比如:比如对象a能够访问 ...

  7. DS博客作业03--栈和队列

    1.本周学习总结 本周学习中学习了栈和队列,栈和队列都属于线性结构,栈和队列不同于线性表的地方在于它们的相关运算具有一些特殊性,所以栈和队列也称为操作受限的线性表. 1.栈 栈是重要且常用的数据结构之 ...

  8. CC++语法::数组名退化(array decaying)

    参考: CSDN::C/C++中数组名退化为指针的情况 stackoverflow::What is array decaying? 起因 笔者在写memset的时候总想偷一点懒(因为我们一般都是为了 ...

  9. Hall定理 二分图完美匹配

    充分性证明就先咕了,因为楼主太弱了,有一部分没看懂 霍尔定理内容 二分图G中的两部分顶点组成的集合分别为X, Y(假设有\(\lvert X \rvert \leq \lvert Y \rvert\) ...

  10. Transformer【Attention is all you need】

    前言 Transfomer是一种encoder-decoder模型,在机器翻译领域主要就是通过encoder-decoder即seq2seq,将源语言(x1, x2 ... xn) 通过编码,再解码的 ...