关于这几个连接查询,举例一看就清清楚楚了
id | name |
1 | 张三 |
2 | 李四 |
3 | 王五 |
4 | 赵六 |
math | english | id |
66 | 87 | 2 |
77 | 65 | 3 |
88 | 54 | 6 |
99 | 65 | 7 |
99 | 75 | 9 |
那么下面就一个个来说明:
1.交叉查询(笛卡尔积)
select * from table1,table2 这个查询出来的数据应该为20*5的表,由于这个比较长,我就不具体例举出来。笛卡尔积应该是很简单很好理解的。
2.内连接
select t1.*,t2.* from table1 t1,table t2 wheret1.id=t2.id;
或者selec t1.*,t2.* from table1 t1 inner jion table t2 on t1.
id | name | math | english | id |
2 | 李四 | 66 | 87 | 2 |
3 | 王五 | 77 | 65 | 3 |
3.左外连接
select t1.*,t2.* from table1 t1,table t2 where t1.id=t2.id(+);
id | name | math | english | id |
1 | 张三 | null | null | null |
2 | 李四 | 66 | 87 | 2 |
3 | 王五 | 77 | 65 | 3 |
4 | 赵六 | null | null | null |
select t1.*,t2.* from table1 t1,table t2 where t1.id(+)=t2.id;
id | name | math | english | id |
2 | 李四 | 66 | 87 | 2 |
3 | 王五 | 77 | 65 | 3 |
null | null | 88 | 54 | 6 |
null | null | 99 | 65 | 7 |
null | null | 99 | 75 | 9 |
select t1.*,t2.* from table1 t1 full outer jion table2 t2ont1.id=t2.id;
id | name | math | english | id |
1 | 张三 | null | null | null |
2 | 李四 | 66 | 87 | 2 |
3 | 王五 | 77 | 65 | 3 |
4 | 赵六 | null | null | null |
null | null | 88 | 54 | 6 |
null | null | 99 | 65 | 7 |
null | null | 99 | 75 | 9 |