码迷,mamicode.com
首页 > 数据库 > 详细

mysql查询,left join(求并集),where(求交集)

时间:2017-09-26 19:27:40      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:mysql语法

准备(两张表t1,t2):

表t1:

mysql> select * from t1;

+-------+---------+

| t1_id | t1_name |

+-------+---------+

|     1 | t1_1    |

|     2 | t1_2    |

+-------+---------+

2 rows in set (0.02 sec)

表t2:

mysql> select * from t2;

+-------+---------+

| t2_id | t2_name |

+-------+---------+

|     2 | t2_2    |

|     3 | t2_3    |

+-------+---------+

2 rows in set (0.03 sec)

并集实例:

mysql> select t1.t1_id,t2.t2_id,t1.t1_name from t1 left join t2 on t1.t1_id=t2.t2_id;

+-------+-------+---------+

| t1_id | t2_id | t1_name |

+-------+-------+---------+

|     1 | NULL  | t1_1    |

|     2 |     2 | t1_2    |

+-------+-------+---------+

2 rows in set (0.02 sec)

交集实例:

mysql> select t1.t1_id,t2.t2_id,t1.t1_name from t1,t2 where t1.t1_id=t2.t2_id;

+-------+-------+---------+

| t1_id | t2_id | t1_name |

+-------+-------+---------+

|     2 |     2 | t1_2    |

+-------+-------+---------+

1 row in set (0.01 sec)


mysql查询,left join(求并集),where(求交集)

标签:mysql语法

原文地址:http://12173069.blog.51cto.com/12163069/1968871

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!