日期:2014-05-16  浏览次数:20501 次

数据库的各种连接查询你是否真了解

下面就让我们来做个小测试,一一探查常用数据库的连接原理。
--数据测试环境准备:
SQL> create table t1(a int);
 
Table created
SQL> create table t2(b int);

Table created
SQL> insert into t1 values(1);
 
1 row inserted
SQL> insert into t1 values(2);
 
1 row inserted
SQL> insert into t1 values(3);
 
1 row inserted
SQL> insert into t2 values(1);
 
1 row inserted
SQL> insert into t2 values(2);
 
1 row inserted
SQL> insert into t2 values(4);
 
1 row inserted
 
SQL> commit;
 
Commit complete


--内连接,符合条件的行列出,可以看作是两个表的交集
SQL> select t1.*,t2.* from t1 inner join t2 on t1.a=t2.b;
 
                                      A                                       B
--------------------------------------- ---------------------------------------
                                      1                                       1
                                      2                                       2
 
--交叉连接,即返回两个表的笛卡尔积
SQL> select t1.*,t2.* from t1 cross join t2;
 
                                      A                                       B
--------------------------------------- ---------------------------------------
                                      1                                       1
                                      1                                       2
                                 &nb