多个字段最常见的值

时间:2010-01-16 23:05:02

标签: mysql database

我有一张包含评分的表格。评级中包含用户提供的数据,例如链接系列所属的语言。

id  series_id  language  quality  type  rating
1   18         1         2        1     20
2   18         2         3        2     13
3   18         1         1        2     25
4   18         1         3        5     8
5   18         3         1        1     17
6   18         3         3        2     9

找到某个series_id的语言,质量和类型最常用的值的最佳方法是什么?

换句话说,如果我查看系列18,它会返回以下内容,因为这些数字最多。

language: 1
quality:  3
type:     2

1 个答案:

答案 0 :(得分:3)

最简单的做法是将它作为三个单独的SELECT语句:

SELECT language
FROM MyTable
WHERE series_id = 18
GROUP BY language
ORDER BY COUNT(*) DESC LIMIT 1

SELECT quality
FROM MyTable
WHERE series_id = 18
GROUP BY quality
ORDER BY COUNT(*) DESC LIMIT 1

SELECT type
FROM MyTable
WHERE series_id = 18
GROUP BY type
ORDER BY COUNT(*) DESC LIMIT 1