0
大家好,這是我的第一篇文章,在給了我很多工作之後,我的頭只是不想再合作了,我在網上搜索了答案, t查找相關案例,我在laravel 5.2中使用查詢工作,表示查詢是獲取項目進度並計算共享相同進度的項目以餅圖顯示。對已分組查詢的結果進行分組sql/laravel
關係如下:
我有3個表:
- 項目
- 步驟來完成(每個人都有一個百分比)。 每一步每一個項目的
- 進度(已完成true或false)
現在我有這樣的代碼
table('items as i')
->join('items_sectors as is', 'i.sector_id', '=', 'is.id')
->join('sectors_steps_progress as stp', 'i.id', '=', 'stp.item_id')
->join('sectors_steps as cs', 'stp.step_id', '=', 'cs.id')
->where('stp.completed', true)
->selectRaw('count(distinct i.id) as count, CONCAT(sum(cs.percentage), "%") as percentage')
->groupBy('stp.completed', 'i.id');
而這與一些測試數據輸出
array:3 [▼
0 => {#2042 ▼
+"count": 1
+"percentage": "50%"
}
1 => {#2043 ▼
+"count": 1
+"percentage": "100%"
}
2 => {#2044 ▼
+"count": 1
+"percentage": "100%"
}
]
我想要現在加入所有的比賽「百分比」,並數它們,所以它會最終如下:
個array:3 [▼
0 => {#2042 ▼
+"count": 1
+"label": "50%"
}
1 => {#2043 ▼
+"count": 2
+"label": "100%"
}
]
非常感謝,如果任何人都可以幫我這個,我還在努力弄清楚:(
我對此採取了類似的方法,是的它創造了奇蹟,所以我將這標記爲一個有效的答案,非常感謝您的答案! –
不客氣 – oseintow