sql – 使用破坏的子选择查询应该导致错误但返回行
发布时间:2020-05-24 10:09:51 所属栏目:MsSql 来源:互联网
导读:我不明白这种情况下的行为.根据我的理解,使用无效子查询的查询应该会导致错误.但在此示例中,它返回一些行. 测试数据: create table test_values ( tst_id number, tst_id2 number, tst_value varchar2( 10 ) );create table test_lookup ( tst_id number, ts
|
我不明白这种情况下的行为.根据我的理解,使用无效子查询的查询应该会导致错误.但在此示例中,它返回一些行. 测试数据: 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,'b' ); insert into test_values( tst_id,tst_value ) values ( 2,'c' ); insert into test_values( tst_id,'d' ); insert into test_lookup( tst_id,'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”-table中获取“test_id2” – 列,而不是从子查询中所述的“test_lookup”-table中获取,尽管不使用内部和外部的别名部分. 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
*/
解决方法原因是,当子查询中不存在unaliased列但外部查询中确实存在时,Oracle假定您引用外部查询中的列.使用别名,您感到困惑的查询将如下所示: select *
from test_values tv
where tv.tst_id in (select tv.tst_id2
from test_lookup tl
where tl.tst_value = 'findMe');
希望这会让事情变得更清楚? 您所看到的问题是一个非常好的示例,说明为什么您应该始终使用它们来自哪个表来标记列 – 这样可以更容易地维护查询以便开始! (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在SQL Server中为特定记录生成脚本
- sql – 当更新另一个表中的列时,创建一个更新一个表上的列的
- sql-server – 分区键是否也必须是主键的一部分?
- php使用pdo连接sqlserver示例分享
- .net – 针对SQL等效的LINQ查询的性能
- 存储 – 关系数据库 – 必须有更多的权利?
- CentOS 7 中以命令行方式安装 MySQL 5.7.11 for Linux Gene
- sql-server – 用于INSERT和UPDATE的SQL Server存储过程,更
- 使用带有SQL Server索引的INCLUDE列有什么好处?
- SQL Server 自动备份数据库并压缩的批处理脚本用法
推荐文章
站长推荐
热点阅读
