mysql的inner join, left join, right join

本文深入解析SQL中的三种连接类型:inner join、left join和right join的使用方法与区别。通过具体示例,展示了如何利用on和where子句来精确控制连接结果,帮助读者更好地理解和运用SQL连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      0. 先看表数据:

mysql> select * from a;
+----+-----+
| id | age |
+----+-----+
|  1 |  10 |
|  2 |  20 |
|  3 |  30 |
|  4 |  40 |
+----+-----+
4 rows in set (0.00 sec)

mysql> select * from b;
+----+-------+
| id | score |
+----+-------+
|  1 |   100 |
|  2 |   200 |
|  3 |   300 |
|  5 |   500 |
+----+-------+
4 rows in set (0.00 sec)

 

     1. inner join最简单,我们之前说过, 来看下:

mysql> select * from a inner join b;
+----+-----+----+-------+
| id | age | id | score |
+----+-----+----+-------+
|  1 |  10 |  1 |   100 |
|  2 |  20 |  1 |   100 |
|  3 |  30 |  1 |   100 |
|  4 |  40 |  1 |   100 |
|  1 |  10 |  2 |   200 |
|  2 |  20 |  2 |   200 |
|  3 |  30 |  2 |   200 |
|  4 |  40 |  2 |   200 |
|  1 |  10 |  3 |   300 |
|  2 |  20 |  3 |   300 |
|  3 |  30 |  3 |   300 |
|  4 |  40 |  3 |   300 |
|  1 |  10 |  5 |   500 |
|  2 |  20 |  5 |   500 |
|  3 |  30 |  5 |   500 |
|  4 |  40 |  5 |   500 |
+----+-----+----+-------+
16 rows in set (0.00 sec)

    可以用on/where过滤下:

mysql> select * from a inner join b on a.id = b.id;
+----+-----+----+-------+
| id | age | id | score |
+----+-----+----+-------+
|  1 |  10 |  1 |   100 |
|  2 |  20 |  2 |   200 |
|  3 |  30 |  3 |   300 |
+----+-----+----+-------+
3 rows in set (0.00 sec)
mysql> select * from a inner join b where a.id = b.id;
+----+-----+----+-------+
| id | age | id | score |
+----+-----+----+-------+
|  1 |  10 |  1 |   100 |
|  2 |  20 |  2 |   200 |
|  3 |  30 |  3 |   300 |
+----+-----+----+-------+
3 rows in set (0.00 sec)

      之所以可以用where, 是因为where之前本身就有结果。

 

     2. 再看left join:

mysql> select * from a left join b;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

     所以很显然不能用where:

mysql> select * from a left join b where a.id = b.id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where a.id = b.id' at line 1

      只能用on:

mysql> select * from a left join b on a.id = b.id;
+----+-----+------+-------+
| id | age | id   | score |
+----+-----+------+-------+
|  1 |  10 |    1 |   100 |
|  2 |  20 |    2 |   200 |
|  3 |  30 |    3 |   300 |
|  4 |  40 | NULL |  NULL |
+----+-----+------+-------+
4 rows in set (0.00 sec)

 

     3. right join和left join类似,来看看right join的结果:

mysql> select * from a right join;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql> select * from a right join where a.id = b.id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where  a.id = b.id' at line 1
mysql> select * from a right join b on a.id = b.id;
+------+------+----+-------+
| id   | age  | id | score |
+------+------+----+-------+
|    1 |   10 |  1 |   100 |
|    2 |   20 |  2 |   200 |
|    3 |   30 |  3 |   300 |
| NULL | NULL |  5 |   500 |
+------+------+----+-------+
4 rows in set (0.00 sec)

 

 

    一目了然,不必多说。

 

### MySQLLEFT JOININNER JOINRIGHT JOIN 的区别及用法 #### 一、基本概念 在 SQL 查询中,`JOIN` 是一种用于将多个表的数据组合在一起的操作。根据不同的需求可以选择不同类型的 `JOIN` 来实现特定的功能。 1. **INNER JOIN(内连接)** - 只返回两个表中满足连接条件的记录[^1]。 - 如果某条记录在一个表中有对应,在另一个表中却没有对应的记录,则该记录不会被包含在最终结果集中。 2. **LEFT JOIN(左连接)** - 返回左表中的所有记录以及右表中满足连接条件的记录[^2]。 - 若右表中不存在与左表匹配的记录,则右表的相关字段会被填充为 `NULL` 值。 3. **RIGHT JOIN(右连接)** - 返回右表中的所有记录以及左表中满足连接条件的记录[^3]。 - 若左表中不存在与右表匹配的记录,则左表的相关字段会被填充为 `NULL` 值。 - 特别注意的是,可以通过调整表的位置,利用 `LEFT JOIN` 实现 `RIGHT JOIN` 的功能[^3]。 #### 二、具体用法示例 ##### 示例数据准备 假设有两张表 `table_a` 和 `table_b`: | table_a.id | table_a.name | |------------|--------------| | 1 | Alice | | 2 | Bob | | table_b.id | table_b.age | |------------|-------------| | 1 | 25 | | 3 | 30 | ##### 使用 INNER JOIN 查询语句如下: ```sql SELECT a.id, a.name, b.age FROM table_a AS a INNER JOIN table_b AS b ON a.id = b.id; ``` - 结果只包含两表中 `id` 字段相等的记录。 - 输出结果: ``` id | name | age ---|-------|---- 1 | Alice | 25 ``` ##### 使用 LEFT JOIN 查询语句如下: ```sql SELECT a.id, a.name, b.age FROM table_a AS a LEFT JOIN table_b AS b ON a.id = b.id; ``` - 左表 (`table_a`) 所有记录都会显示出来;如果右表 (`table_b`) 没有匹配项,则其字段值为 `NULL`。 - 输出结果: ``` id | name | age ---|-------|----- 1 | Alice | 25 2 | Bob | NULL ``` ##### 使用 RIGHT JOIN 查询语句如下: ```sql SELECT a.id, a.name, b.age FROM table_a AS a RIGHT JOIN table_b AS b ON a.id = b.id; ``` - 右表 (`table_b`) 所有记录都会显示出来;如果左表 (`table_a`) 没有匹配项,则其字段值为 `NULL`。 - 输出结果: ``` id | name | age ---|-------|----- 1 | Alice | 25 NULL| NULL | 30 ``` #### 三、性能比较 关于 `LEFT JOIN` 和 `INNER JOIN` 的执行速度问题,通常情况下两者差异不大。但在某些场景下可能会有所不同。这是因为当使用 `ON` 子句指定过滤条件时,生成了一个中间临时表,而后续再应用 `WHERE` 子句进一步筛选数据[^4]。对于 `INNER JOIN` 而言,无论条件写在 `ON` 或者 `WHERE` 中,最终影响到的结果是一致的。 --- ###
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值