【问题标题】:Failed to run a query with case when ... then ... else ... end, with postgres无法使用 case when ... then ... else ... end 运行查询,使用 postgres
【发布时间】:2018-05-24 15:30:18
【问题描述】:

我正在将进程从 oracle 迁移到 postgres,并且验证解码不存在,我必须使用 case when ... then ... else ... end。

问题是在进行查询时,我在语法中遇到错误,但研究了一下,语法与我在下面显示的相同。

select case when (substr(min(debit.tprp_codigo), 1, 1) = 'S' then 'sorry' else 'F' end) 
from debit;

显示我的错误如下

ERROR: syntax error on or near «then»
LINE 1: ... when (substr(min(debit.tprp_codigo), 1, 1) = 'S' then 'sorr...

SQL state: 42601
Character: 62

我尝试在括号()中将...括起来,但它仍然显示相同的错误

select debi_correlativo,
       (case when (substr(min(debit.tprp_codigo), 1, 1) = 'S' then 'sorry' else 'F' end))
from debit;

【问题讨论】:

    标签: sql postgresql case


    【解决方案1】:

    最好去掉去掉所有不必要的括号:

    case when substr(min(debit.tprp_codigo), 1, 1) = 'S' then 'sorry' else 'F' end
    

    应该可以。


    如果您坚持使用无用的括号,请将 whole case 表达式放在它们之间:

    (case when substr(min(debit.tprp_codigo), 1, 1) = 'S' then 'sorry' else 'F' end) 
    

    条件:

    case when (substr(min(debit.tprp_codigo), 1, 1) = 'S') then 'sorry' else 'F' end 
    

    【讨论】:

      【解决方案2】:

      有两种编写案例查询的方法。在您的情况下,额外的括号会导致语法错误。

      1.:

      case when substr(min(debit.tprp_codigo), 1, 1) = 'S' then 'sorry' else 'F' end
      

      2.:

      case substr(min(debit.tprp_codigo), 1, 1) when 'S' then 'sorry' else 'F' end
      

      【讨论】:

        猜你喜欢
        • 2011-08-05
        • 2013-12-04
        • 2012-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多