mysql ifelse流程控制语句
程序员文章站
2024-01-05 12:19:16
...
mysql ifelse流程控制语句
-
简单判断
当 status=1时返回1,否则0 :select id, if(status=1, 1, 0) as source from table_1
IF(表达式1, 表达式2 ,表达式3)
如果表达式1为true(表达式1 <> 0 and 表达式1 <> NULL), 返回表达式2;否则返回表达式3;相当于三目运算符mysql> SELECT IF(1>2,2,3); -> 3 mysql> SELECT IF(1<2,'yes','no'); -> 'yes'
-
多条件语句
select id,
(
CASE
WHEN qty_1 <= '23' THEN price
WHEN '23' > qty_1 && qty_2 <= '23' THEN price_2
WHEN '23' > qty_2 && qty_3 <= '23' THEN price_3
WHEN '23' > qty_3 THEN price_4
ELSE 1
END
) AS total
from product;
上一篇: R中如何用ifelse进行数据分组