这是一个关于如何直接在MySQL中执行此操作的简单示例.您当然需要在子查询上添加一个条件,以便仅获得相关项目的投票而不是所有投票.
mysql> create table votes( vote int); Query OK, 0 rows affected (0.01 sec) mysql> insert into votes values (5),(5),(4); Query OK, 3 row affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from votes; +------+ | vote | +------+ | 5 | | 5 | | 4 | +------+ 3 rows in set (0.00 sec) mysql> select vote,count(vote),vote*count(vote) from votes group by vote; +------+-------------+------------------+ | vote | count(vote) | vote*count(vote) | +------+-------------+------------------+ | 4 | 1 | 4 | | 5 | 4 | 20 | +------+-------------+------------------+ 2 rows in set (0.00 sec) mysql> select sum(vt)/sum(cnt) FROM (select count(vote)*count(vote) as cnt,vote*count(vote)*count(vote) as vt from votes group by vote) a; +------------------+ | sum(vt)/sum(cnt) | +------------------+ | 4.8000 | +------------------+ 1 row in set (0.00 sec)