假设服务按以下方式结算:
以下是示例计算:
是否有可能仅在MySQL中进行这种计算?
编辑1:
这样的事情是否有效:
SELECT `call_length`,
( 1.00 + ( Round(( `call_length` - 30 ) / 10) * .25 ) ) AS `cost`
FROM `service`
答案 0 :(得分:2)
<强> SqlFiddleDemo 强>
CREATE TABLE sec(val INT);
INSERT INTO sec
VALUES (32), (59), (60), (61), (69), (70), (71);
SELECT
val,
1.0 + CASE
WHEN val <= 60.0 THEN 0
WHEN val MOD 10 = 0 THEN 0.25 *((val - 60) DIV 10)
ELSE 0.25 * (((val - 60) DIV 10) + 1)
END AS charge
FROM sec;
没有CASE
:
<强> SqlFiddleDemo2 强>
SELECT
call_length,
1.0 + IF( call_length <= 60, 0, 0.25 * CEIL((call_length - 60)/10)) AS cost
FROM service;
答案 1 :(得分:0)
这不是一个MySQL问题,除非您需要执行计算的设置有些困难(?)。
UPDATE ... SET cost_cents = 100 + CEIL(GREATEST(0, duration - 60)/10) * 25;
作为匹配您的修改的SELECT
,
SELECT `call_length`,
100 + CEIL(GREATEST(0, `call_length` - 60)/10) * 25 AS `cost`
FROM `service`
请注意,这会返回美分。对于美元,将结果除以100 ......
SELECT `call_length`,
(100 + CEIL(GREATEST(0, `call_length` - 60)/10) * 25) / 100 AS `cost`
FROM `service`