标量子查询只和外连接语句等价
点击(此处)折叠或打开
- --下面的语句是否等价?
- SELECT t1.ID, t2.ID FROM t1, t2 WHERE t1.ID = t2.ID;
- SELECT t1.ID, (SELECT t2.ID FROM t2 WHERE ID = t1.ID) FROM t1;
-
- --构造环境,进行验证
- DROP TABLE t1;
- CREATE TABLE t1 (ID INT);
- INSERT INTO t1 VALUES (1);
- INSERT INTO t1 VALUES (2);
- INSERT INTO t1 VALUES (3);
- COMMIT;
-
- DROP TABLE t2;
- CREATE TABLE t2 (ID INT );
- INSERT INTO t2 VALUES (1);
- INSERT INTO t2 VALUES (2);
- INSERT INTO t2 VALUES (NULL);
- COMMIT;
-
- --执行如下,发现两者并不等价。
- SELECT t1.ID, t2.ID FROM t1, t2 WHERE t1.ID = t2.ID;
- ID ID
- ---------- ----------
- 1 1
- 2 2
-
- SELECT t1.ID, (SELECT t2.ID FROM t2 WHERE ID = t1.ID) FROM t1;
-
- ID (SELECTT2.IDFROMT2WHEREID=T1.ID)
- ---------- --------------------------------
- 1 1
- 2 2
- 3
-
- --实际上标量子查询只和外关联语句完全等价,具体试验如下:
- SELECT t1.ID, t2.ID FROM t1, t2 WHERE t1.ID = t2.ID(+);
-
- ID ID
- ---------- ----------
- 1 1
- 2 2
- 3
- SELECT t1.ID, (SELECT t2.ID FROM t2 WHERE ID = t1.ID) FROM t1;
-
- ID (SELECTT2.IDFROMT2WHEREID=T1.ID)
- ---------- --------------------------------
- 1 1
- 2 2
- 3
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/17013648/viewspace-1071303/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/17013648/viewspace-1071303/