单行子查询 return 超过一行 - 当查询应该 return 1 行时
single-row subquery returns more than one row - when query should return 1 row
我希望使用以下输入从以下查询中得到一个结果对象。
但是我收到这个错误:
ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
数据为:
id ssn Name Branch City DepntSsn
----------------------------------------------
01 100 AAA IT CA 101
02 101 BBB RA VA 104
03 101 BBB RA VA 104
Oracle 查询:
SELECT
id,
ssn,
name,
branch,
city,
depntssn,
CASE WHEN branch == 'IT' then (select id from case where ssn = DepntSsn) else null end DepntId
FROM
case
WHERE
ssn = 100
)
WHERE
ROWNUM = 1
;
输出应该是:
id ssn Name Branch City DepntSsn DepntId
-----------------------------------------------------
01 100 AAA IT CA 101 02
错误似乎很明显。此子查询:
(select id from case where ssn = DepntSsn)
returns不止一行。嗯,原因可能跟这个是一样的table,所以这是真的在做:
(select c2.id from case c2 where c2.ssn = c2.DepntSsn)
这肯定不是您想要的。我怀疑你想要这样的东西:
select . . .,
(select c2.id from case c2 where c2.ssn = c.DepntSsn)
from case c
. . .
当您的查询有多个 table 引用时,您应该 always 限定 all 列引用。这对于关联子查询尤为重要!
此外,case
是 table 的糟糕名称,因为它是 SQL 关键字。
我希望使用以下输入从以下查询中得到一个结果对象。
但是我收到这个错误:
ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
数据为:
id ssn Name Branch City DepntSsn
----------------------------------------------
01 100 AAA IT CA 101
02 101 BBB RA VA 104
03 101 BBB RA VA 104
Oracle 查询:
SELECT
id,
ssn,
name,
branch,
city,
depntssn,
CASE WHEN branch == 'IT' then (select id from case where ssn = DepntSsn) else null end DepntId
FROM
case
WHERE
ssn = 100
)
WHERE
ROWNUM = 1
;
输出应该是:
id ssn Name Branch City DepntSsn DepntId
-----------------------------------------------------
01 100 AAA IT CA 101 02
错误似乎很明显。此子查询:
(select id from case where ssn = DepntSsn)
returns不止一行。嗯,原因可能跟这个是一样的table,所以这是真的在做:
(select c2.id from case c2 where c2.ssn = c2.DepntSsn)
这肯定不是您想要的。我怀疑你想要这样的东西:
select . . .,
(select c2.id from case c2 where c2.ssn = c.DepntSsn)
from case c
. . .
当您的查询有多个 table 引用时,您应该 always 限定 all 列引用。这对于关联子查询尤为重要!
此外,case
是 table 的糟糕名称,因为它是 SQL 关键字。