概念

  • 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. [Cordova inAppBrowser 在App内打开浏览器]

    方案1: 使用Cordova插件 cordova-plugin-inappbrowser 1.  添加插件 cordova plugin add cordova-plugin-inappbrowser ...

  2. BigDecimal(大浮点数)

    因为这个单词,和他的四则运算方法总是记不住,所以写入博客,在没有印象的时候再看看自己的博客. BigDecimal的加减乘除不和double float 一样,他需要使用方法来进行加减乘除. 加法:a ...

  3. js判断手机端

    if (window.location.toString().indexOf('pref=padindex') != -1) { } else { if (/AppleWebKit.*Mobile/i ...

  4. Python Face Detect Offline

    python版本 3.7.0  1. 安装 cmake pip install cmake  2.安装 boost pip install boost  3.安装 dlib pip install d ...

  5. Get Remote Computer Install Software

    #requires -Version 2 function Get-Software { param ( [string] $DisplayName='*', [string] $UninstallS ...

  6. 使用systemback制作Ubuntu自定义系统镜像和系统备份(抄)

    使用systemback制作Ubuntu自定义系统镜像和系统备份 2017年06月23日 16:17:51 BWBOT 阅读数:10714   原链接:https://community.bwbot. ...

  7. Go语言中DateTime知识点

    一.基本使用 ①从属于time这个包 ②一般使用都是使用 time.Time 这个类型表示时间 ,time包中还有一些常量,源码如下 // Common durations. There is no ...

  8. python学习day20 面向对象(二)类成员&成员修饰符

    1.成员 类成员 类变量 绑定方法 类方法 静态方法 属性 实例成员(对象) 实例变量 1.1实例变量 类实例化后的对象内部的变量 1.2类变量 类中的变量,写在类的下一级和方法同一级. 访问方法: ...

  9. [CTSC2017]网络

    [CTSC2017]网络 连一条长度为len的边,使得基环树的直径最小 结论:一定连在某条直径两个点上(否则更靠近不劣) 然后二分答案判定. dp[i]:链上一个点往下延伸的最大深度 考虑对于任意两个 ...

  10. 初探VUX(基本官网上无特别无干货)

    vux@2.x 推荐webpack+vue-loader方式的开发. 第一步安装cli依赖 npm install vue-cli -g 接下来创建项目注意名称是小写 cd projectPath y ...