如何使用MySQL的开始时间,结束时间和每小时价格来计算每小时的价格?
这是我此时的要求:
$query = "SELECT
requests.id as request_id,
requests.request_type as request_type,
service_types.name as service_type,
requests.status,
requests.provider_status,
requests.amount,
requests.s_latitude as latitude,
requests.s_longitude as longitude,
requests.confirmed_provider as provider_id,
requests.user_accepted,
requests.provider_accepted,
providers.id as provider_id,
providers.first_name as provider_first_name,
providers.last_name as provider_last_name,
providers.picture as provider_picture,
AVG(provider_ratings.rating) as rating,
provider_services.price as price_per_hour,
requests.start_time,
TIMEDIFF(now(),requests.start_time) as time_elapsed_since_start,
TIMEDIFF(requests.end_time,requests.start_time) as total_request_time,
(TIMEDIFF(requests.end_time,requests.start_time) * provider_services.price) / 10000 as price_before_tax,
provider_services.price / TIMEDIFF(requests.end_time,requests.start_time) as price_with_tax
FROM
requests
LEFT JOIN
providers ON providers.id = requests.confirmed_provider
LEFT JOIN
provider_ratings ON requests.confirmed_provider = provider_ratings.provider_id
LEFT JOIN
provider_services ON providers.id = provider_services.provider_id AND provider_services.service_type_id = requests.request_type
LEFT JOIN
service_types ON requests.request_type = service_types.id
WHERE
(requests.user_id = $request->id AND requests.provider_status != 1 AND requests.status != 2 AND requests.status != 6 AND requests.user_accepted = 1 AND requests.provider_accepted = 1) OR
(requests.user_id = $request->id AND requests.provider_status != 0 AND requests.status != 0 AND requests.status != 6 AND requests.user_accepted = 1 AND requests.provider_accepted = 1)
GROUP BY
requests.id";
此时计算价格:
(TIMEDIFF(requests.end_time,requests.start_time) * provider_services.price) / 10000 as price_before_tax,
只有当请求需要整整几个小时时才能正常运行,例如:
start_time:2017-11-21 14:21:16
end_time:2017-11-21 16:21:16
但是如果我将end_time更改为 2017-11-21 16:51:16 ,那么计算出的金额就像你在那里看到的那样疯狂:
答案 0 :(得分:1)
基于分钟计算时,计算会更加安全。 像这样。
查询start_time:2017-11-21 14:21:16 end_time:2017-11-21 16:21:16
SELECT TIMESTAMPDIFF(MINUTE, '2017-11-21 14:21:16', '2017-11-21 16:21:16') * (30 / 60)
<强>结果强>
TIMESTAMPDIFF(MINUTE, '2017-11-21 14:21:16', '2017-11-21 16:21:16') * (30 / 60)
---------------------------------------------------------------------------------
60.0000
查询start_time:2017-11-21 14:21:16 end_time:2017-11-21 16:51:16
SELECT TIMESTAMPDIFF(MINUTE, '2017-11-21 14:21:16', '2017-11-21 16:51:16') * (30 / 60)
<强>结果强>
TIMESTAMPDIFF(MINUTE, '2017-11-21 14:21:16', '2017-11-21 16:51:16') * (30 / 60)
---------------------------------------------------------------------------------
75.0000
答案 1 :(得分:0)
以下是我解决问题的方法:
$query = "SELECT
requests.id as request_id,
requests.request_type as request_type,
service_types.name as service_type,
requests.status,
requests.provider_status,
requests.amount,
requests.s_latitude as latitude,
requests.s_longitude as longitude,
requests.confirmed_provider as provider_id,
requests.user_accepted,
requests.provider_accepted,
providers.id as provider_id,
providers.first_name as provider_first_name,
providers.last_name as provider_last_name,
providers.picture as provider_picture,
AVG(provider_ratings.rating) as rating,
provider_services.price as price_per_hour,
requests.start_time,
requests.end_time,
TIMEDIFF(requests.end_time,requests.start_time) as total_request_time,
TIME_TO_SEC(TIMEDIFF(requests.end_time,requests.start_time))/60 as total_request_time_in_minutes,
TIMEDIFF(now(),requests.start_time) as time_elapsed_since_start,
(TIME_TO_SEC(now()) - TIME_TO_SEC(requests.start_time))/60 as time_elapsed_since_start_in_minutes,
TIME_TO_SEC(TIMEDIFF(now(),requests.start_time))/60 as time_elapsed_since_start_in_minutes,
IF((TIME_TO_SEC(TIMEDIFF(requests.end_time,requests.start_time))/60) > 15 ,(TIME_TO_SEC(TIMEDIFF(requests.end_time,requests.start_time))/60) * (provider_services.price/60),15 * (provider_services.price/60)) as total_price,
IF((TIME_TO_SEC(TIMEDIFF(requests.end_time,requests.start_time))/60) > 15 ,(TIME_TO_SEC(TIMEDIFF(requests.end_time,requests.start_time))/60),15) as total_time
FROM
requests
LEFT JOIN
providers ON providers.id = requests.confirmed_provider
LEFT JOIN
provider_ratings ON requests.confirmed_provider = provider_ratings.provider_id
LEFT JOIN
provider_services ON providers.id = provider_services.provider_id AND provider_services.service_type_id = requests.request_type
LEFT JOIN
service_types ON requests.request_type = service_types.id
WHERE
(requests.user_id = $request->id AND requests.provider_status != 1 AND requests.status != 2 AND requests.status != 6 AND requests.user_accepted = 1 AND requests.provider_accepted = 1) OR
(requests.user_id = $request->id AND requests.provider_status != 0 AND requests.status != 0 AND requests.status != 6 AND requests.user_accepted = 1 AND requests.provider_accepted = 1)
GROUP BY
requests.id";
$requests = DB::select(DB::raw($query));