很抱歉这个新手问题,我不知道如何将这个mysql查询派生到laravel query-builder中,请检查我的sql查询。预先感谢大家
SELECT vehicle_monitoring.id AS monitoring_id,
Sum(vehicle_monitoring_details.distance_travelled) AS total_distance,
Sum(CASE
WHEN vehicle_monitoring_details.purpose = 'Business' THEN
vehicle_monitoring_details.distance_travelled
END) AS distance_business,
Sum(CASE
WHEN vehicle_monitoring_details.purpose = 'Private' THEN
vehicle_monitoring_details.distance_travelled
END) AS distance_private
FROM vehicle_monitoring
JOIN vehicle_monitoring_details
ON vehicle_monitoring_details.vehicle_monitoring_id =
vehicle_monitoring.id
GROUP BY vehicle_monitoring.id
答案 0 :(得分:0)
尝试一下。我没有对此进行测试,但是应该可以正常工作。请问您是否有任何澄清或问题:
DB::table('vehicle_monitoring')
->join('vehicle_monitoring_details',
'vehicle_monitoring_details.vehicle_monitoring_id',
'=', 'vehicle_monitoring.id')
->groupBy('vehicle_monitoring.id')
->select('vehicle_monitoring.id AS monitoring_id',
DB::Raw('Sum(vehicle_monitoring_details.distance_travelled) AS total_distance'),
DB::Raw("Sum(CASE
WHEN vehicle_monitoring_details.purpose = 'Business' THEN
vehicle_monitoring_details.distance_travelled
END) AS distance_business"),
DB::Raw("Sum(CASE
WHEN vehicle_monitoring_details.purpose = 'Private' THEN
vehicle_monitoring_details.distance_travelled
END) AS distance_private")
)->get();