简体   繁体   English

基于列值的 IF 和 ELSE 语句

[英]IF and ELSE Statement based on column value

Based on one column within my query results (Value), I am trying to write an if/else statement based on the value held which will display the result the in an additional row.基于我的查询结果(值)中的一列,我试图根据所持有的值编写一个 if/else 语句,该语句将在另一行中显示结果。

For example, if I have a record of 2 within the value field, but I want to check whether it is above < 5. If the value is less than 5 I basically want the additional column to display a hardcoded value of 5, else display actual value.例如,如果我在 value 字段中有 2 的记录,但我想检查它是否在 < 5 以上。如果值小于 5 我基本上希望附加列显示硬编码值 5,否则显示实际价值。

Any help would be appreciated.任何帮助,将不胜感激。

Use a case statement使用case语句

select a.*, 
       case
           when a.TheField < 5 then 5
           else a.TheField
       end as NewField
from MyTable a

You can use a case你可以使用一个case

select value, case when value < 5 
                   then 5 
                   else value 
              end as calculated_column
from your_table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM