我有一个带有百分比的表(值为0-100) (让我们假设表name=TableA中只有1列名为%)。
我想要做的是在班级中划分百分比。例如,假设是width=2类,我的最后结果是:
percent | quantity
------------------
[0-2) | 5
[2-4) | 43
... | ...
[96-98) | 232
[98-100]| 173我已经考虑了一些方法,但我需要一个有效的方法,考虑到该表包含大约500.000行。最后,我将用php创建一个图表,所以我并不关心解决方案是基于mysql的还是基于php的。
(嗯,这肯定不是最合适的,但我正在写,这样我就能清楚地说明我想要什么)。
select count (*) from tableA where percent>=0 and percent<2;
select count (*) from tableA where percent>=2 and percent<4;
...2)使用php读取每一行并对其进行分类
select * from tableA
<?php while ($row=...)
$class[intval($row[0]/50)]++; ?>3)创建一个表TableB,类似于:
floor | roof
------------
0 | 2
2 | 4
4 | 6
... | ...然后
select tableB.floor/2,count(*)
from tableA join tableB
on tableA.percent>=tableB.floor and tableA.percent<tableB.roof
group by tableB.floor/2我更喜欢第二个,虽然我不确定用php还是用mysql进行计算比较好。
你有什么更好的主意吗?
发布于 2014-01-24 11:52:14
您可以使用公式创建直方图桶,而无需第二个表的帮助。
SELECT count(*), FLOOR(percent/2)*2 percent
FROM tableA
GROUP BY FLOOR(percent/2)*2
ORDER BY FLOOR(percent/2)*2这应该非常有效,特别是在您的percent列上有索引的情况下。
例如,percent值18和19将由组按公式转换为18。
https://stackoverflow.com/questions/21331784
复制相似问题