這裏是我的表關係:Laravel SQL GROUPBY,總結
**reservations** id, date, etc...<br>
**reservation_service_info** service_info_id, reservation_id<br>
**services_info** price etc...
我想白天總結預訂價格。下面是代碼和結果:
return $query->selectRaw('DATE(start_time) AS date')
->selectRaw("(SELECT SUM(price) FROM `services_info` inner join `reservation_service_info` on `services_info`.`id` = `reservation_service_info`.`service_info_id` where `reservation_service_info`.`reservation_id` = reservations.id) as price")
->orderBy('date', 'ASC')
->get('price', 'date')
結果:
[
{
"date": "2016-06-01",
"price": "345.00"
},
{
"date": "2016-06-01",
"price": "90.00"
},
{
"date": "2016-06-01",
"price": "222.00"
},
{
"date": "2016-06-02",
"price": "393.00"
},
{
"date": "2016-06-02",
"price": "142.00"
}
]
當我在查詢這組是由dosent SUM(價格)加groupBy('date')
[
{
"date": "2016-06-01",
"price": "345.00"
},
{
"date": "2016-06-02",
"price": "393.00"
}
]
是的,我試過,但我得到同樣的效果,因爲這個選擇查詢只是選擇一列... – DokiCRO
@ user3806764你可以編輯你的問題,並顯示期望的結果呢? –
因爲我現在改變了。 – DokiCRO