0

这个问题已经问了好几次了,但从来没有得到真正的答案!

我有一个看起来像这样的表:

answer_id  q_id  answer  qscore_id  answer_timestamp
1          10    Male    3          1363091016
2          10    Male    3          1363091017
3          11    Male    3          1363091018
4          10    Male    3          1363091019
5          11    Male    3          1363091020
6          12    Male    3          1363091020
7          11    Male    3          1363091025

因此,对于相同的问题(q_id),我有多个答案。我希望每个问题只能检索一个答案,这是最新的答案。

IE。

应该返回三行,它们都是该 q_id 的最新回答:

4          10    Male    3          1363091019
6          12    Male    3          1363091020
7          11    Male    3          1363091020

任何帮助将非常感激。

谢谢

4

2 回答 2

3

Use a subselect to get the max timestamps and then join that back against the table

SELECT a.answer_id, a.q_id, a.answer, a.qscore_id, a.answer_timestamp
FROM SomeTable a
INNER JOIN (SELECT q_id, MAX(answer_timestamp) AS answer_timestamp FROM SomeTable GROUP BY q_id) Sub1
ON a.q_id = Sub1.q_id AND a.answer_timestamp = Sub1.answer_timestamp
于 2013-03-13T09:27:10.667 回答
-2

从 q_id=? 的表中选择 * 按 answer_timestamp desc 限制 3 排序

于 2013-03-13T09:24:59.663 回答