我将此表命名为rating:
id | helpful | necesary | lenght
1 | 3 | 1 | 5
1 | 4 | 3 | 2
2 | 5 | 3 | 5
3 | 3 | 3 | 5
1 | 1 | 2 | 3
3 | 2 | 3 | 2
此表存储用户从1到5给出的评级....我怎样才能查询评分最高的前5个ID,平均3个列有用,必要,长度?
AVG给了我一列的平均值,LIMIT给了我前五名,但我无法开始如何平均三列。
答案 0 :(得分:0)
最简单的方法是从列中添加值并将它们除以列数。
SELECT (helpful + necesary + length) / 3 AS "avg" FROM rating ORDER BY avg DESC LIMIT 5;
答案 1 :(得分:0)
您可以手动汇总值,按该总和排序,然后将总和除以列数以获得平均值:
select id, helpful, necesary, lenght,
((helpful + necesary + lenght) / 3) as avg
from rating
order by (helpful + necesary + lenght) desc
limit 5
答案 2 :(得分:0)
也许你想这样做:
SELECT id FROM (
SELECT id, (helpful + necesary + legnth) / 3 average
FROM rating ORDER BY average DESC
) t
LIMIT 5;