您在问题中提到的查询代替了另一个...主查询中包含的标量子查询。我格式化了整个查询(为了便于阅读),它看起来像这样:
SELECT
(
select case when coalesce(table1.col1, table2.col2,table1.col3,
table1.col4) is null
then (select sysdate from dual)
else coalesce(table1.col1, table2.col2,table1.col3, table1.col4)
end
from table1
join table2 on table1.id = table2.id
) as "ProgressDate",
table3.id as "ID"
FROM table3, table1, table2, table4
WHERE table3.transaction = #{inputvaluepassed}
AND table1.id = table3.id
AND table2.id=table1.id and table2.action = table4.action
现在,根据定义,标量子查询只能返回零或一行。在您的情况下,似乎在运行时此子查询返回多行,并且主查询崩溃。
您最多需要以某种方式生成一行:也许通过聚合行(使用GROUP BY
),也许通过仅从结果集中选择一行(使用LIMIT
);还有其他选择。如果我们选择将行数限制为最多 1 行,您的查询可能如下所示:
SELECT
(
select case when coalesce(table1.col1, table2.col2,table1.col3,
table1.col4) is null
then (select sysdate from dual)
else coalesce(table1.col1, table2.col2,table1.col3, table1.col4)
end
from table1
join table2 on table1.id = table2.id
limit 1 -- added this line
) as "ProgressDate",
table3.id as "ID"
FROM table3, table1, table2, table4
WHERE table3.transaction = #{inputvaluepassed}
AND table1.id = table3.id
AND table2.id=table1.id and table2.action = table4.action
这只是该问题的一种可能的廉价解决方案。更好地理解如何在多个行中选择正确的行可以产生更好的解决方案。