【Mysql】if判断和case when 记录总结

if表达式

if(expr1, expr2, expr3)

如果 expr1 是true ,则 if的返回值为expr2; 否则返回值则为 expr3。


case when 用法

1、第一种用法

case条件判断的变量

    when条件判断的变量的值 then执行语句

    when条件判断的变量的值 then执行语句

    else执行语句

end

case order.settlement_ratio
  when 0 then (order.buyer_unit_price - order.seller_unit_price - order.platform_rebates_money) * order.actual_weight * 0.5
  else (order.buyer_unit_price - order.seller_unit_price - order.platform_rebates_money) * order.actual_weight * order.settlement_ratio
end as actual_income

2、第二种用法

case

    when 条件 then 执行语句

    when 条件 then 执行语句

    else 执行语句

end

case
  when order.settlement_ratio = 0 and order.weight_counting = 1 and order.before_actual_weight > 0
    then (order.buyer_unit_price - order.seller_unit_price - order.platform_rebates_money) * order.before_actual_weight * 0.5
  when order.settlement_ratio = 0 and order.weight_counting = 1 and order.before_actual_weight = 0
    then (order.buyer_unit_price - order.seller_unit_price - order.platform_rebates_money) * order.weight * 0.5
  when order.settlement_ratio != 1 and order.weight_counting = 1 and order.before_actual_weight > 0
    then (order.buyer_unit_price - order.seller_unit_price - order.platform_rebates_money) * order.before_actual_weight * order.settlement_ratio
  when order.settlement_ratio != 1 and order.weight_counting = 1 and order.before_actual_weight = 0
    then (order.buyer_unit_price - order.seller_unit_price - order.platform_rebates_money) * order.weight * order.settlement_ratio
  else (order.buyer_unit_price - order.seller_unit_price - order.platform_rebates_money) * order.weight * order.settlement_ratio
end as income

 

 

你可能感兴趣的:(mysql)