这是我的表:
date | capacity
10/09/13 | 100
10/09/13 | 50
10/09/13 | 100
08/09/13 | 100
低容量为50,高容量为100。
我正在尝试检索每种容量类型每天的平均容量。 所以预期的结果是:
10/09/13
low capacity average: 50
high capacity average: 100
08/09/13
low capacity average: 0
high capacity average: 100
我目前的询问是:
选择日期,容量FROM表GROUP BY日期,容量
为了在阵列中呈现自定义容量类型(低,高),我应该添加什么?
类似的东西:
$array = [['date'=>'10/09/13','high_capacity'=>100,'low_capacity'=>50],
['date'=>'08/09/13','high_capacity'=>100,'low_capacity'=>0]];
答案 0 :(得分:0)
这可以为你做的工作:
SELECT date, AVG(capacity) FROM table GROUP BY date ;
或者,如果您仍希望检索最小值和最大值:
SELECT
date,
MIN(capacity) as low_capacity,
MAX(capacity) as high_capacity,
AVG(capacity) as average_capacity
FROM table
GROUP BY date ;