当前位置:操作系统 > Unix/Linux >>

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


数据库的各种连接查询你是否真了解
 
下面就让我们来做个小测试,一一探查常用数据库的连接原理。
--数据测试环境准备:
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);
   www.zzzyk.com  
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
                                      1                                       4
                                      2                                       1
                                      2                                       2
                                      2                                       4
                                      3                                       1
                                      3                                       2
                                      3                                       4
 
--最常用的连接查询,即从两个表的笛卡尔积两进行条件过滤,功能等同inner join,但是效率要底于inner join
SQL> select t1.*,t2.* from t1, t2 where t1.a=t2.b;
 
                                      A                                       B
--------------------------------------- ---------------------------------------
                                      1                                       1
                                      2                                       2
--全连接,符合条件行列出,不符合条件的行也列出,可以看作是两个表的交集
SQL> select t1.*,t2.* from t1 full join t2 on t1.a=t2.b;
 
         A          B
---------- ----------
         1          1
         2          2
         3 
                    4
  www.zzzyk.com  
--以最左边表的行数为基准,左表的记录将会全部表示出来,而右表只会显示符合搜索条件的记录,右表记录不足的地方均为NULL
SQL> select t1.*,t2.* from t1 left outer join t2 on t1.a=t2.b;
 
                                      A                                       B
--------------------------------------- ---------------------------------------
                                      1                                       1
                                      2                          
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,