if函数作用:
mysql中if函数是一个控制流函数,可以根据一个条件返回一个值。
if函数语法:
IF(expr,if_true_expr,if_false_expr)
if函数使用方法:
mysql中if函数通常与sun函数组合使用
SELECTSUM(IF(status = 'Shipped', 1, 0)) AS Shipped,SUM(IF(status = 'Cancelled', 1, 0)) AS CancelledFROMorders;
SELECT
SUM(IF(status = 'Shipped', 1, 0)) AS Shipped,
SUM(IF(status = 'Cancelled', 1, 0)) AS Cancelled
FROM
orders;