您好,欢迎访问一九零五行业门户网

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

数据库的各种连接查询你是否真了解 下面就让我们来做个小测试,一一探查常用数据库的连接原理。 --数据测试环境准备: 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> 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.2cto.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.2cto.com  
--以最左边表的行数为基准,左表的记录将会全部表示出来,而右表只会显示符合搜索条件的记录,右表记录不足的地方均为null
sql> select t1.*,t2.* from t1 left outer join t2 on t1.a=t2.b;
a                                       b
--------------------------------------- ---------------------------------------
                                      1                                       1
                                      2                                       2
                                      3
--同上
sql> select t1.*,t2.* from t1 left join t2 on t1.a=t2.b;
a                                       b
--------------------------------------- ---------------------------------------
                                      1                                       1
                                      2                                       2
                                      3 
   www.2cto.com  
--以最右边表的行数为基准,右表的记录将会全部表示出来,而左表只会显示符合搜索条件的记录,左表记录不足的地方均为null
sql> select t1.*,t2.* from t1 right outer join t2 on t1.a=t2.b;
a                                       b
--------------------------------------- ---------------------------------------
                                      1                                       1
                                      2                                       2
                                                                              4
--同上
sql> select t1.*,t2.* from t1 right join t2 on t1.a=t2.b;
a                                       b
--------------------------------------- ---------------------------------------
                                      1                                       1
                                      2                                       2
                                                                              4
注意了:以后各种连接可不要乱用了哦。
其它类似信息

推荐信息