关键词搜索

源码搜索 ×
×

mysql的inner join, left join, right join

发布2019-06-15浏览7366次

详情内容

      0. 先看表数据:

  1. mysql> select * from a;
  2. +----+-----+
  3. | id | age |
  4. +----+-----+
  5. | 1 | 10 |
  6. | 2 | 20 |
  7. | 3 | 30 |
  8. | 4 | 40 |
  9. +----+-----+
  10. 4 rows in set (0.00 sec)
  11. mysql> select * from b;
  12. +----+-------+
  13. | id | score |
  14. +----+-------+
  15. | 1 | 100 |
  16. | 2 | 200 |
  17. | 3 | 300 |
  18. | 5 | 500 |
  19. +----+-------+
  20. 4 rows in set (0.00 sec)

 

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

  1. mysql> select * from a inner join b;
  2. +----+-----+----+-------+
  3. | id | age | id | score |
  4. +----+-----+----+-------+
  5. | 1 | 10 | 1 | 100 |
  6. | 2 | 20 | 1 | 100 |
  7. | 3 | 30 | 1 | 100 |
  8. | 4 | 40 | 1 | 100 |
  9. | 1 | 10 | 2 | 200 |
  10. | 2 | 20 | 2 | 200 |
  11. | 3 | 30 | 2 | 200 |
  12. | 4 | 40 | 2 | 200 |
  13. | 1 | 10 | 3 | 300 |
  14. | 2 | 20 | 3 | 300 |
  15. | 3 | 30 | 3 | 300 |
  16. | 4 | 40 | 3 | 300 |
  17. | 1 | 10 | 5 | 500 |
  18. | 2 | 20 | 5 | 500 |
  19. | 3 | 30 | 5 | 500 |
  20. | 4 | 40 | 5 | 500 |
  21. +----+-----+----+-------+
  22. 16 rows in set (0.00 sec)

    可以用on/where过滤下:

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

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

 

     2. 再看left join:

  1. mysql> select * from a left join b;
  2. 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:

  1. mysql> select * from a left join b where a.id = b.id;
  2. 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:

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

 

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

  1. mysql> select * from a right join;
  2. 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
  1. mysql> select * from a right join where a.id = b.id;
  2. 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
  1. mysql> select * from a right join b on a.id = b.id;
  2. +------+------+----+-------+
  3. | id | age | id | score |
  4. +------+------+----+-------+
  5. | 1 | 10 | 1 | 100 |
  6. | 2 | 20 | 2 | 200 |
  7. | 3 | 30 | 3 | 300 |
  8. | NULL | NULL | 5 | 500 |
  9. +------+------+----+-------+
  10. 4 rows in set (0.00 sec)

 

 

    一目了然,不必多说。

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载