如何将我的 sql 查询编码为 Laravel 查询
How can I code my sql query to Laravel query
如何将以下 sql 查询转换为 Laravel 查询?
SELECT roster_date, COUNT(DISTINCT truck_id) AS available_units
FROM truck_rosters
WHERE(truck_rosters.status = 'Active')
分组依据 roster_date
并与此查询合并:
SELECT roster_date, COUNT(DISTINCT truck_id) AS total_units
来自 truck_rosters
分组依据 roster_date
预期结果:
roster_date | total_units | available_units
2019-08-30 | 11 | 3个
2019-08-31 | 10 | 4
对两个计数使用条件聚合:
$rosters = DB::table('truck_rosters')
->select(DB::raw("COUNT(DISTINCT truck_id) AS total_units,
COUNT(CASE WHEN status = 'Active' THEN 1 END) AS active_units, roster_date"))
->groupBy('roster_date')
->get();
如何将以下 sql 查询转换为 Laravel 查询?
SELECT roster_date, COUNT(DISTINCT truck_id) AS available_units
FROM truck_rosters
WHERE(truck_rosters.status = 'Active')
分组依据 roster_date
并与此查询合并:
SELECT roster_date, COUNT(DISTINCT truck_id) AS total_units 来自 truck_rosters 分组依据 roster_date
预期结果:
roster_date | total_units | available_units
2019-08-30 | 11 | 3个 2019-08-31 | 10 | 4
对两个计数使用条件聚合:
$rosters = DB::table('truck_rosters')
->select(DB::raw("COUNT(DISTINCT truck_id) AS total_units,
COUNT(CASE WHEN status = 'Active' THEN 1 END) AS active_units, roster_date"))
->groupBy('roster_date')
->get();