mysql中join,left join,right join 的区别
先看例子:
首先是join
select vend_name ,prod_price,prod_name from products join vendors on vendors.vend_id = products.vend_id;
+-----------------+------------+---------------------+| vend_name | prod_price | prod_name |+-----------------+------------+---------------------+| Bears R Us | 5.99 | 8 inch teddy bear || Bears R Us | 8.99 | 12 inch teddy bear || Bears R Us | 11.99 | 18 inch teddy bear || Doll House Inc. | 3.49 | Fish bean bag toy || Doll House Inc. | 3.49 | Bird bean bag toy || Doll House Inc. | 3.49 | Rabbit bean bag toy || Doll House Inc. | 4.99 | Raggedy Ann || Fun and Games | 9.49 | King doll || Fun and Games | 9.49 | Queen doll |+-----------------+------------+---------------------+
select vend_name ,prod_price,prod_name from products left join vendors on vendors.vend_id = products.vend_id;
+-----------------+------------+---------------------+| vend_name | prod_price | prod_name |+-----------------+------------+---------------------+| Doll House Inc. | 3.49 | Fish bean bag toy || Doll House Inc. | 3.49 | Bird bean bag toy || Doll House Inc. | 3.49 | Rabbit bean bag toy || Bears R Us | 5.99 | 8 inch teddy bear || Bears R Us | 8.99 | 12 inch teddy bear || Bears R Us | 11.99 | 18 inch teddy bear || Doll House Inc. | 4.99 | Raggedy Ann || Fun and Games | 9.49 | King doll || Fun and Games | 9.49 | Queen doll |+-----------------+------------+---------------------+
select vend_name ,prod_price,prod_name from products right join vendors on vendors.vend_id = products.vend_id;
+-----------------+------------+---------------------+| vend_name | prod_price | prod_name |+-----------------+------------+---------------------+| Bear Emporium | NULL | NULL || Bears R Us | 5.99 | 8 inch teddy bear || Bears R Us | 8.99 | 12 inch teddy bear || Bears R Us | 11.99 | 18 inch teddy bear || Doll House Inc. | 3.49 | Fish bean bag toy || Doll House Inc. | 3.49 | Bird bean bag toy || Doll House Inc. | 3.49 | Rabbit bean bag toy || Doll House Inc. | 4.99 | Raggedy Ann || Fun and Games | 9.49 | King doll || Fun and Games | 9.49 | Queen doll || Furball Inc. | NULL | NULL || Jouets et ours | NULL | NULL |+-----------------+------------+---------------------+