运行"if else"查询



我运行以下查询:

select field1 f1, field2 f2, case when field3 >1 and field 4 > 0 than field3 else field4 as f34, field5 f5

这个查询不起作用(我在大小写附近得到语法错误)在sql中实现if-else的正确方法是什么?

试试这个:

select field1 f1, field2 f2, 
   (case when field3 >1 and field 4 > 0 then field3 
    else field4 end) as f34, 
   field5 f5

你有一个打字错误(than),我也将其格式化为更易于阅读。

FROM丢失,则情况的END(而不是)丢失:

SELECT field1 f1
    ,field2 f2
    ,CASE 
        WHEN field3 > 1
            AND field4 > 0
            THEN field3
        ELSE field4
        END AS f34
    ,field5 f5
FROM TABLE

我认为这是一个拼写错误,请尝试

SELECT field1 f1, field2 f2, (case when field3 >1 and field 4 > 0 then field3 else field4) as f34, field5 f5 FROM table

一些打字错误,并且没有用END:关闭CASE

select field1 f1, 
       field2 f2, 
       case when field3 >1 and field 4 > 0 then 
           field3 
       else 
           field4 
       end as f34, 
       field5 f5

最新更新