在MySQL查询中返回分组的组

时间:2014-09-11 12:11:18

标签: mysql

我有一个MySQL表'tbl_words',如下所示:

+----+-------+---------+
| id | name  | word    |
+----+-------+---------+
|  1 | Bob   | rabbit  |
|  2 | Bob   | hare    |
|  3 | Bob   | hare    |
|  4 | Bob   | chicken |
|  5 | Paul  | goose   |
|  6 | Paul  | chicken |
|  7 | Paul  | rabbit  |
|  8 | Paul  | hare    |
|  9 | Paul  | hare    |
| 10 | Paul  | chicken |
| 11 | Paul  | goose   |
| 12 | Laura | goose   |
| 13 | Laura | rabbit  |
| 14 | Laura | rabbit  |
| 15 | Laura | goose   |
| 16 | Laura | fox     |
+----+-------+---------+

我想为每个列'name'获得两个最常见的列'word'。我可以通过查询获得的最接近的是:

SELECT name, word, COUNT(*) AS magnitude 
FROM tbl_words 
GROUP BY name, word 
ORDER BY magnitude DESC
LIMIT 2

返回:

+-------+---------+-----------+
| name  | word    | magnitude |
+-------+---------+-----------+
| Paul  | chicken |         2 |
| Laura | rabbit  |         2 |
+-------+---------+-----------+

但是我正在寻找这个:

+-------+---------+-----------+
| name  | word    | magnitude |
+-------+---------+-----------+
| Bob   | hare    |         2 |
| Bob   | chicken |         1 |
| Paul  | chicken |         2 |
| Paul  | goose   |         2 |
| Laura | rabbit  |         2 |
| Laura | goose   |         2 |
+-------+---------+-----------+

我如何实现这一目标,即获得每个名字最常用的两个单词?

3 个答案:

答案 0 :(得分:0)

我还没有通过测试,但您的查询应该如下:

    SELECT name, word, COUNT(*) AS magnitude 
    FROM tbl_words where name in 
(select name from   tbl_words ORDER BY magnitude DESC LIMIT 2)
    GROUP BY name, word 

答案 1 :(得分:0)

你想要每组最大的n,所以只需要计算它并按照这样的方式进行过滤

SELECT name, word, magnitude 
FROM(
    SELECT name, word, magnitude, IF(@a = name, @b := @b + 1, @b := 1) counting, @a := name
    FROM(
        SELECT name, word, COUNT(*) AS magnitude 
        FROM tbl_words 
        GROUP BY name, word
        ORDER BY name, magnitude DESC
    ) t
    CROSS JOIN(select @a := '', @b := 0)temp
)t1
WHERE counting <=3

DEMO

答案 2 :(得分:0)

此查询将返回每个用户每个单词的出现次数:

SELECT name, word, COUNT(*) as cnt
FROM tbl_words
GROUP BY name, word

然后我们可以在这个子查询上使用GROUP_CONCAT,由cnt desc命令:

SELECT name, GROUP_CONCAT(word ORDER BY cnt DESC)
FROM (
  SELECT name, word, COUNT(*) as cnt
  FROM tbl_words
  GROUP BY name, word
) s
GROUP BY name

将返回以逗号分隔的所有单词列表,按cnt降序排列。

然后您可以使用SUBSTRING_INDEX获取前两个单词:

SELECT name, SUBSTRING_INDEX(GROUP_CONCAT(word ORDER BY cnt DESC), ',', 2) most_frequent
FROM (
  SELECT name, word, COUNT(*) as cnt
  FROM tbl_words
  GROUP BY name, word
) s
GROUP BY name

请记住,如果两个以上的单词具有相同的频率,则会选择两个随机单词。