1

我在我的查询中使用了 coalesce mybatis 开关盒,我遇到了类似的错误

查询数据库时出错。原因:java.sql.SQLException:ORA-01427:单行子查询返回多于一行

这是我的查询

(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))

提前致谢

4

2 回答 2

0

似乎你有很多 () 但总的来说你应该使用 = 运算符而不是 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

查看您在示例中发布的代码,您有一个选择作为列结果,并且此选择返回几行,(这会引发错误)。您还可以混合连接语法,一些基于显式连接语法,一些基于旧的隐式连接语法,基于逗号分隔的表名和 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> 
于 2020-07-24T19:37:45.323 回答
0

您在问题中提到的查询代替了另一个...主查询中包含的标量子查询。我格式化了整个查询(为了便于阅读),它看起来像这样:

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

这只是该问题的一种可能的廉价解决方案。更好地理解如何在多个行中选择正确的行可以产生更好的解决方案。

于 2020-07-24T21:08:38.593 回答