【发布时间】:2017-10-10 05:25:55
【问题描述】:
有没有办法从答案表中为每个 Q 选择前 2 个答案:
user_answers 表结构:
id question_id user_id answer_id create_date
1 1 9 5 null
2 2 8 7 null
3 1 1 3 null
4 3 4 20 null
5 1 4 5 null
6 4 3 25 null
7 2 7 5 null
8 4 9 26 null
9 2 5 8 null
10 1 1 5 null
我需要返回这样的结果:
id question_id user_id answer_id create_date
1 1 9 5 null
3 1 1 3 null
2 2 8 7 null
7 2 7 5 null
4 3 4 20 null
6 4 3 25 null
8 4 9 26 null
这就像 Group by "question_id" 但从每个组中选择前 2 行,
谢谢,
【问题讨论】:
-
first 2 rows...您如何定义“第一”? -
表“id”列(ASC)
-
"这就像 Group by "question_id""" -- 不,它不像
GROUP BY。GROUP BY不从表中返回行;它使用来自每个组的数据计算它返回的行的内容。这个问题属于标签greatest-n-per-group -
谢谢你告诉我,那么有没有办法在 MySQL 中处理它?