【发布时间】:2009-08-23 14:10:15
【问题描述】:
我真的很难弄清楚这一点。
我有一个表格“评论”:
cmt_id (primary key, auto incr), thread_id, cmt_text
我有这些记录:
cmt_id thread_id cmt_txt
5002 1251035762511 Alright, I second this.
5003 1251036148894 Yet another comment.
5001 1251035762511 I am starting a thread on this.
我现在想,在每个线程中获得最少的cmt_id 记录。所以,我用这种方式做了一个聚合查询:
SELECT cmt_id, thread_id, cmt_text, MIN(cmt_id) FROM comments
GROUP BY thread_id;
但是,我最终得到了这个:
cmt_id thread_id cmt_text MIN(cmt_id)
5002 1251035762511 Alright, I second this. 5001
5003 1251036148894 Yet another comment. 5003
对于带有thread_id“1251035762511”的线程,我总是收到带有cmt_id5002 的评论作为具有最小评论ID 的记录。我什至尝试插入新记录,cmt_id5002 总是以 MIN 和不是cmt_id 5001 的记录。
【问题讨论】:
标签: sql mysql database group-by