mysql的join方式 join是连接查询两个表的一种方式,会把两个表符合条件的行拼到一起形成一个新表并返回。 对于两个集合,join可以生成七种情况,如下。
下面分别对这七种情况介绍sql语句。
情况1
这种情况直接用inner join就可以。
* from tableA A inner join tableB B on A.key 1 2 3 4 5 6 7 ## 情况2  直接left join。 ```select * from tableA A left join tableB B on A.key = B.key;
情况3
在情况2的基础上,去掉B的部分,所以只需要加一个条件 B.key is null 即可。
* from tableA A left join tableB B on A.key 1 2 3 4 5 6 7 ## 情况4和情况5 下面两种和left join是对应的。  ```select * from tableA A right join tableB B on A.key = B.key;
* from tableA A right join tableB B on A.key 1 2 3 4 5 6 7 8 9 10 ## 情况6  把left join和right join给union起来,union还会自动去重处理重合的部分。 ```sql select * from tableA A left join tableB B on A.key = B.key union select * from tableA A left join tableB B on A.key = B.key
情况7
在情况6下递推,用情况3和情况5连接。
1 2 3 select * from tableA A left join tableB B on A.key = B.key where B.key is null union select * from tableA A right join tableB B on A.key = B.key where B.key is null ;