sql - ORA-01427单行子查询返回多于 1 行
问题描述
这个错误似乎很流行,并且有很多相关的答案。但是,现有的答案似乎不适用于我的情况。
我正在使用 2 个表来简化我的案例:Test1 和 Test3(参见插图)
我想要做的是尝试在 test3 表中找到与字段 value1 中的值不匹配的记录(如果字段 check_condition1 = 1,如果它是 0,那么我不在乎)
所以基本上结果应该类似于这个特定场景中的这个查询:
select distinct t3.* from test3 t3, test1 t1
where t3.department=t1.department
and t3.value1 not in ('A','B');
但是,如果我使用此语句:
select distinct t3.* from test3 t3, test1 t1
where t3.department=t1.department
and t3.value1 not in
(
case t1.CHECK_CONDITION1
when 0 then
(select '1' from dual where 1=2)
when 1 then
( select value1 from test1 where department=t3.DEPARTMENT)
end
)
我收到了这条消息:
ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
*Cause:
*Action:
我认为我的子查询“从 test1 中选择 value1 where department=t3.DEPARTMENT”应该返回一个 t3.value1 的集合以进行检查。
应如何更正该说法?我的目标是使用 Test1 表作为控制表,字段 Check_condition1、check_condition2 是可以打开和关闭的“开关”,而无需更改主查询。请告知我的想法是否有意义。
附件是创建表 test1 和 test3 的脚本,以便更轻松地复制我的问题。
CREATE TABLE "TEST1"
( "DEPARTMENT" NUMBER(3,0),
"VALUE1" VARCHAR2(26 BYTE),
"VALUE2" VARCHAR2(26 BYTE),
"CHECK_CONDITION1" NUMBER(3,0),
"CHECK_CONDITION2" NUMBER(3,0)
)
Insert into TEST1 (DEPARTMENT,VALUE1,VALUE2,CHECK_CONDITION1,CHECK_CONDITION2) values (1,'A','Z',1,0);
Insert into TEST1 (DEPARTMENT,VALUE1,VALUE2,CHECK_CONDITION1,CHECK_CONDITION2) values (1,'B','Y',1,0);
CREATE TABLE "TEST3"
( "DEPARTMENT" NUMBER(3,0),
"VALUE1" VARCHAR2(26 BYTE),
"VALUE2" VARCHAR2(26 BYTE),
"VALUE3" VARCHAR2(26 BYTE)
);
Insert into TEST3 (DEPARTMENT,VALUE1,VALUE2,VALUE3) values (1,'A','T','Whatever');
Insert into TEST3 (DEPARTMENT,VALUE1,VALUE2,VALUE3) values (1,'Z','Y','Whatever');
Insert into TEST3 (DEPARTMENT,VALUE1,VALUE2,VALUE3) values (1,'B','Y','Whatever');
解决方案
那将是这样的,我想:
SQL> select distinct t3.*
2 from test3 t3 join test1 t1 on t3.department=t1.department
3 where t3.value1 not in
4 (select t1.value1 from test1 t1
5 where t1.department = t3.department
6 and 1 = case when t1.check_condition1 = 1 then 1
7 else 0
8 end
9 );
DEPARTMENT VALUE1 VALUE2 VALUE3
---------- ---------- ---------- ----------
1 Z Y Whatever
SQL>
如果条件为 0,你说你不在乎,所以:
SQL> update test1 set check_condition1 = 0;
2 rows updated.
SQL> select distinct t3.*
2 from test3 t3 join test1 t1 on t3.department=t1.department
3 where t3.value1 not in
4 (select t1.value1 from test1 t1
5 where t1.department = t3.department
6 and 1 = case when t1.check_condition1 = 1 then 1
7 else 0
8 end
9 );
DEPARTMENT VALUE1 VALUE2 VALUE3
---------- ---------- ---------- ----------
1 B Y Whatever
1 A T Whatever
1 Z Y Whatever
SQL>
推荐阅读
- c# - 如何将内容放在 docx 的 DOCVARIABLE 字段中
- java - 使用 FileDialog 选择多个文件不起作用
- node.js - 如何将我的项目从 ionic 3.9.2 版本升级到 ionic 4 最新版本
- omnet++ - 从 Veins 4.7.1 检索车辆价值
- c++ - 如何在 C++ 中通过引用返回向量?
- javascript - 如何重构将 const 对象名称添加到数组中?
- video - 他们是一种防止来自 Jwplayer 的 ts 视频片段被嗅探的方法吗?
- webpack - 如何修复参数数量不足或找不到条目。或者,运行 'webpack(-cli) --help' 以获取使用信息
- terminal - 如何修复 EACCES:权限被拒绝,在安装 gatsby-cli 时打开 '/Users/....
- php - FOSRestBundle 与 BazingaHateoasBundle 集成,但响应中仍然缺少链接