多次问过这个问题,但从未收到过实际答案!
我有一张看起来像这样的表:
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
任何帮助都会非常感激。
由于
答案 0 :(得分:3)
使用子选择获取最大时间戳,然后将其连接到表
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
答案 1 :(得分:-2)
从表中选择*,其中q_id =?按answer_timestamp desc limit 3
排序