ANSI sql join

http://psoug.org/

Cross Join 又成为笛卡尔连接

CREATE TABLE table_one (
  col_one NUMBER,
  col_two VARCHAR2(10)
);
 
CREATE TABLE table_two (
  col_three NUMBER,
  col_four  VARCHAR2(10)
);
 
INSERT INTO table_one VALUES ( 1,    'one');
INSERT INTO table_one VALUES ( 2,    'two');
 
INSERT INTO table_two VALUES (10,    'ten');
INSERT INTO table_two VALUES (20, 'twenty');
INSERT INTO table_two VALUES ( 5,   'five');
 
SELECT * FROM
  table_one cross join
  table_two;
 
 
-- Each row from table_one is returned together 
-- with each row from table_two:
 
   COL_ONE COL_TWO     COL_THREE COL_FOUR
---------- ---------- ---------- ----------
         1 one                10 ten
         1 one                20 twenty
         1 one                 5 five
         2 two                10 ten
         2 two                20 twenty
         2 two                 5 five
 



Full Join
CREATE TABLE left_tbl (
  id  NUMBER,
  txt VARCHAR2(10)
);
 
CREATE TABLE right_tbl (
  id  NUMBER,
  txt VARCHAR2(10)
);
 
  INSERT INTO  left_tbl VALUES (1, 'one'   );
  INSERT INTO  left_tbl VALUES (2, 'two'   );
  INSERT INTO  left_tbl VALUES (3, 'three' );
--insert into  left_tbl values (4, 'four'  );
  INSERT INTO  left_tbl VALUES (5, 'five'  );
 
  INSERT INTO right_tbl VALUES (1, 'uno'   );
--insert into right_tbl values (2, 'dos'   );
  INSERT INTO right_tbl VALUES (3, 'tres'  );
  INSERT INTO right_tbl VALUES (4, 'cuatro');
  INSERT INTO right_tbl VALUES (5, 'cinco' );
 
 
-- A full join returns the records of both tables 
-- (that satisfy a [potential] where condition). In 
-- the following example, 4 cuatro and 2 two are returned, 
-- although the ids 4 and 2 are not present in both tables:
 
SELECT
             id,
           l.txt,
           r.txt
  FROM
            left_tbl l full join
           right_tbl r using(id)
          id;
 
        ID TXT        TXT
---------- ---------- ----------
         1 one        uno
         2 two
         3 three      tres
         4            cuatro
         5 five       cinco


Inner Join
CREATE TABLE table_one (
  col_one NUMBER,
  col_two NUMBER
);
 
CREATE TABLE table_two (
  col_one NUMBER,
  col_two NUMBER
);
 
INSERT INTO table_one VALUES ( 1, 1);
INSERT INTO table_one VALUES ( 3, 5);
INSERT INTO table_one VALUES ( 5, 9);
 
INSERT INTO table_two VALUES ( 4, 5);
INSERT INTO table_two VALUES ( 6, 3);
INSERT INTO table_two VALUES ( 5, 5);
 
SELECT * FROM 
  table_one t1 inner join 
  table_two t2 ON t1.col_one = t2.col_two;
 
   COL_ONE    COL_TWO    COL_ONE    COL_TWO
---------- ---------- ---------- ----------
         5          9          4          5
         3          5          6          3
         5          9          5          5
 
SELECT * FROM 
  table_one t1 inner join 
  table_two t2 using (col_two);
 
-- Note: col_two is only returned once here instead of twice
-- when using is used instead of on. This is because it must 
-- be the same value:
 
   COL_TWO    COL_ONE    COL_ONE
---------- ---------- ----------
         5          3          4
         5          3          5



Outer Join
CREATE TABLE table_one (
  col_one NUMBER,
  col_two CHAR(1)
);
 
CREATE TABLE table_two (
  col_one NUMBER,
  col_two CHAR(1)
);
 
INSERT INTO table_one VALUES (1, 'a');
INSERT INTO table_one VALUES (2, 'b');
INSERT INTO table_one VALUES (3, 'c');
 
INSERT INTO table_two VALUES (2, 'B');
INSERT INTO table_two VALUES (3, 'C');
INSERT INTO table_two VALUES (4, 'D');
 
SELECT * FROM 
  table_one t1 left outer join
  table_two t2 ON t1.col_one = t2.col_one;
 
   COL_ONE C    COL_ONE C
---------- - ---------- -
         2 b          2 B
         3 c          3 C
         1 a
 
SELECT * FROM 
  table_one t1 right outer join
  table_two t2 ON t1.col_one = t2.col_one;
 
   COL_ONE C    COL_ONE C
---------- - ---------- -
         2 b          2 B
         3 c          3 C
                      4 D
 
SELECT * FROM 
  table_one t1 full outer join
  table_two t2 ON t1.col_one = t2.col_one;
 
   COL_ONE C    COL_ONE C
---------- - ---------- -
         2 b          2 B
         3 c          3 C
         1 a
                      4 D
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值