【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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章