case when then

mysql的case when和java的switch case方法用法相似。如果条件1,就返回结果1。
语法如下:

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END; 

例子:

select OrderId,Quantity,
CASE
WHEN Qualtity>30 THEN "The quantity is greater than 30 "
WHEN Qualtity=30 THEN "The quantity is 30"
ELSE "The quantity is under 30"
END AS QuantityText
FROM OrderDetails;

返回结果:


你可能感兴趣的:(case when then)