MySQL查询向新列添加值

时间:2016-11-03 14:26:42

标签: mysql

尝试进行查询,但如果里程数为>则需要向TotalCost添加250; 150。

select milegae.dc_id, 
mileage.store_id, 
mileage.mileage, 
round((mileage.mileage * .75 + 200),2) as TripCost
from mileage;

此查询为我提供了我想要的数据,但我不确定如何在给定参数的情况下向TotalCost添加250.

2 个答案:

答案 0 :(得分:1)

您可以使用CASE EXPRESSION

执行此操作
select milegae.dc_id, 
    mileage.store_id, 
    mileage.mileage, 
    CASE WHEN mileage.mileage > 150 
         THEN ROUND(((mileage.mileage+250) * .75 + 200),2) 
         ELSE ROUND((mileage.mileage * .75 + 200),2) 
    END as TripCost
from mileage;

答案 1 :(得分:0)

使用CASE表达式检查里程数> 150。

如果里程值大于250,您希望将150添加到最终值。

<强>查询

SELECT 
  milegae.dc_id, 
  mileage.store_id, 
  mileage.mileage,
  CASE WHEN mileage.mileage > 150 
    THEN round((mileage.mileage * .75 + 200),2) + 250
    ELSE round((mileage.mileage * .75 + 200),2) 
  END AS TripCost
FROM mileage;