【发布时间】:2016-04-08 04:32:48
【问题描述】:
我不理解这种情况下的行为。据我了解,带有无效子查询的查询应该会导致错误。但在这个例子中,它返回了一些行。
测试数据:
create table test_values ( tst_id number, tst_id2 number, tst_value varchar2( 10 ) );
create table test_lookup ( tst_id number, tst_value varchar2( 10 ) );
insert into test_values( tst_id, tst_id2, tst_value ) values ( 1, 2, 'a' );
insert into test_values( tst_id, tst_id2, tst_value ) values ( 1, 2, 'b' );
insert into test_values( tst_id, tst_id2, tst_value ) values ( 2, 2,'c' );
insert into test_values( tst_id, tst_id2, tst_value ) values ( 2, 2,'d' );
insert into test_lookup( tst_id, tst_value ) values ( 1, 'findMe' );
commit;
按预期工作:
select * from test_values where tst_id in ( select tst_id from test_lookup where tst_value = 'findMe' );
/*
TST_ID TST_ID2 TST_VALUE
---------- ---------- ----------
1 2 b
1 2 a
*/
select tst_id2 from test_lookup where tst_value = 'findMe';
--ORA-00904: "TST_ID2": invalid identifier
但以下查询也在检索行,显然是通过从“test_values”表中获取“test_id2”列,而不是从子查询中所述的“test_lookup”表中获取,尽管没有使用别名内部和外部。
select * from test_values where tst_id in ( select tst_id2 from test_lookup where tst_value = 'findMe' );
/*
TST_ID TST_ID2 TST_VALUE
---------- ---------- ----------
2 2 c
2 2 d
*/
【问题讨论】:
-
@vmachan OP 发布了一个完整的测试用例,包括创建表和插入语句。我不确定您希望 OP 为他们的问题添加哪些额外信息!
-
我在这里stackoverflow.com/questions/34774242/…问了一个类似的问题。如果您要问如何改变这种行为……看来您不能。
-
是的。它使用正确(但更长)的代码,一切都很好;)
标签: sql oracle subquery correlated-subquery