mysql数据库join、left join与right join 的区别

首先是join 
Sql代码   收藏代码
  1. select vend_name ,prod_price,prod_name from products join vendors on vendors.vend_id = products.vend_id;  

结果 
Sql代码   收藏代码
  1. +-----------------+------------+---------------------+  
  2. | vend_name       | prod_price | prod_name           |  
  3. +-----------------+------------+---------------------+  
  4. | Bears R Us      | 5.99       | 8 inch teddy bear   |  
  5. | Bears R Us      | 8.99       | 12 inch teddy bear  |  
  6. | Bears R Us      | 11.99      | 18 inch teddy bear  |  
  7. | Doll House Inc. | 3.49       | Fish bean bag toy   |  
  8. | Doll House Inc. | 3.49       | Bird bean bag toy   |  
  9. | Doll House Inc. | 3.49       | Rabbit bean bag toy |  
  10. | Doll House Inc. | 4.99       | Raggedy Ann         |  
  11. | Fun and Games   | 9.49       | King doll           |  
  12. | Fun and Games   | 9.49       | Queen doll          |  
  13. +-----------------+------------+---------------------+  

left join 
Sql代码   收藏代码
  1. select vend_name ,prod_price,prod_name from products left   
  2. join vendors on vendors.vend_id = products.vend_id;  

结果 
Sql代码   收藏代码
  1. +-----------------+------------+---------------------+  
  2. | vend_name       | prod_price | prod_name           |  
  3. +-----------------+------------+---------------------+  
  4. | Doll House Inc. | 3.49       | Fish bean bag toy   |  
  5. | Doll House Inc. | 3.49       | Bird bean bag toy   |  
  6. | Doll House Inc. | 3.49       | Rabbit bean bag toy |  
  7. | Bears R Us      | 5.99       | 8 inch teddy bear   |  
  8. | Bears R Us      | 8.99       | 12 inch teddy bear  |  
  9. | Bears R Us      | 11.99      | 18 inch teddy bear  |  
  10. | Doll House Inc. | 4.99       | Raggedy Ann         |  
  11. | Fun and Games   | 9.49       | King doll           |  
  12. | Fun and Games   | 9.49       | Queen doll          |  
  13. +-----------------+------------+---------------------+  

right join的情况呢 
Sql代码   收藏代码
  1. select vend_name ,prod_price,prod_name from products right  
  2.  join vendors on vendors.vend_id = products.vend_id;  

Sql代码   收藏代码
  1. +-----------------+------------+---------------------+  
  2. | vend_name       | prod_price | prod_name           |  
  3. +-----------------+------------+---------------------+  
  4. | Bear Emporium   | NULL       | NULL                |  
  5. | Bears R Us      | 5.99       | 8 inch teddy bear   |  
  6. | Bears R Us      | 8.99       | 12 inch teddy bear  |  
  7. | Bears R Us      | 11.99      | 18 inch teddy bear  |  
  8. | Doll House Inc. | 3.49       | Fish bean bag toy   |  
  9. | Doll House Inc. | 3.49       | Bird bean bag toy   |  
  10. | Doll House Inc. | 3.49       | Rabbit bean bag toy |  
  11. | Doll House Inc. | 4.99       | Raggedy Ann         |  
  12. | Fun and Games   | 9.49       | King doll           |  
  13. | Fun and Games   | 9.49       | Queen doll          |  
  14. | Furball Inc.    | NULL       | NULL                |  
  15. | Jouets et ours  | NULL       | NULL                |  
  16. +-----------------+------------+---------------------+  

总结:join 的结果会和left join或者right join的其一的结果一样,顺序不同,不知道为什么呢,left join 左联结优先考虑左表,即products ,得到的结果行数和products表的行数一样,right join 右联结,有线考虑右表,即vendors ,得到的结果和右表vendors的行数一样,无法匹配的时候用NULL值填充。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值