合并 mybatis 开关盒
coalesce mybatis switch case
我在查询中使用 coalesce mybatis switch case,我收到类似
的错误
Error querying database. Cause: java.sql.SQLException: ORA-01427:
single-row subquery returns more than one row
这是我的查询
(select
(case when (coalesce(t1.col1,t2.col1, t1.col2, t1.col3) is null)
then (select sysdate from dual)
else (coalesce(t1.col1,t2.col1, t1.col2, t1.col3))
end )
from table1 t1
join table2 t2
on t1.id IN (t2.id))
提前致谢
似乎你有很多 () 但总的来说你应该使用 = 运算符而不是 IN (t2.id) 来加入 t2.id
select
case when coalesce(t1.col1,t2.col1, t1.col2, t1.col3) is null
then sysdate
else coalesce(t1.col1,t2.col1, t1.col2, t1.col3)
end
from table1 t1
join table2 t2 on t1.id = t2.id
并且查看您在示例中发布的代码,您有一个 select 作为列结果,这个 select return 几行,(这会引发错误)。您还有一个混合连接语法,一些基于显式连接语法,一些基于旧的隐式连接语法,基于逗号分隔的 table 名称和 where 条件。你应该尝试使用这个
<select id="Trigger" parameterType="hashmap" resultType="java.util.HashMap" flushCache="true">
SELECT
select case when coalesce(table1.col1, table2.col2,table1.col3, table1.col4) is null
then sysdate
else coalesce(table1.col1, table2.col2,table1.col3, table1.col4) end as "ProgressDate"
, table3.id as "ID"
from table1
INNER join table2 on table1.id = table2.id
INNER JOIN table3 ON table1.id = table3.id
INNER JOIN table4 table2.action = table4.action
WHERE table3.transaction = #{inputvaluepassed}
</select>
您在问题中提到的查询取代了另一个...主查询中包含的 标量子查询。我格式化了整个查询(为了便于阅读),它看起来像这样:
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
现在,根据定义,标量子查询只能 return 零行或一行。在您的情况下,该子查询似乎在运行时 returning 多行,并且主查询崩溃。
您最多需要以某种方式生成一行:可能通过聚合行(使用 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
这只是该问题的一种可能的廉价解决方案。更好地了解如何在多个行中选择正确的行可以产生更好的解决方案。
我在查询中使用 coalesce mybatis switch case,我收到类似
的错误Error querying database. Cause: java.sql.SQLException: ORA-01427: single-row subquery returns more than one row
这是我的查询
(select
(case when (coalesce(t1.col1,t2.col1, t1.col2, t1.col3) is null)
then (select sysdate from dual)
else (coalesce(t1.col1,t2.col1, t1.col2, t1.col3))
end )
from table1 t1
join table2 t2
on t1.id IN (t2.id))
提前致谢
似乎你有很多 () 但总的来说你应该使用 = 运算符而不是 IN (t2.id) 来加入 t2.id
select
case when coalesce(t1.col1,t2.col1, t1.col2, t1.col3) is null
then sysdate
else coalesce(t1.col1,t2.col1, t1.col2, t1.col3)
end
from table1 t1
join table2 t2 on t1.id = t2.id
并且查看您在示例中发布的代码,您有一个 select 作为列结果,这个 select return 几行,(这会引发错误)。您还有一个混合连接语法,一些基于显式连接语法,一些基于旧的隐式连接语法,基于逗号分隔的 table 名称和 where 条件。你应该尝试使用这个
<select id="Trigger" parameterType="hashmap" resultType="java.util.HashMap" flushCache="true">
SELECT
select case when coalesce(table1.col1, table2.col2,table1.col3, table1.col4) is null
then sysdate
else coalesce(table1.col1, table2.col2,table1.col3, table1.col4) end as "ProgressDate"
, table3.id as "ID"
from table1
INNER join table2 on table1.id = table2.id
INNER JOIN table3 ON table1.id = table3.id
INNER JOIN table4 table2.action = table4.action
WHERE table3.transaction = #{inputvaluepassed}
</select>
您在问题中提到的查询取代了另一个...主查询中包含的 标量子查询。我格式化了整个查询(为了便于阅读),它看起来像这样:
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
现在,根据定义,标量子查询只能 return 零行或一行。在您的情况下,该子查询似乎在运行时 returning 多行,并且主查询崩溃。
您最多需要以某种方式生成一行:可能通过聚合行(使用 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
这只是该问题的一种可能的廉价解决方案。更好地了解如何在多个行中选择正确的行可以产生更好的解决方案。