Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 分组_Sql_Mysql_Database_Group By - Fatal编程技术网

Sql 分组

Sql 分组,sql,mysql,database,group-by,Sql,Mysql,Database,Group By,我真的很难弄明白这一点 我有一张“评论”表: 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 sta

我真的很难弄明白这一点

我有一张“评论”表:

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\u 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\u id
“1251035762511”的线程,我总是将具有
cmt\u id
5002的注释作为具有最小注释id的记录。。我甚至尝试插入新记录,
cmt\u id
5002总是作为MIN出现,而不是带有
cmt\u id
5001的记录。

这可能只是一个输入错误,但您输入的SQL查询实际上包含MIN(cmt\u id)而不是MAX(cmt\u id)

我想你可能需要在你的班长旁边放一个电话,和他谈谈

如果您只需要max comment id,那么

SELECT MAX(cmt_id), thread_id FROM comments GROUP BY thread_id

就够了。如果需要附加字段,则需要根据此处的其他答案使用子查询

你必须有以下几行:

SELECT *
FROM comments
     INNER JOIN (
       SELECT cmt_id = MIN(cmt_id), thread_id
       FROM comments
       GROUP BY thread_id
     ) cmin ON cmin.cmt_id = comments.cmt_id
SELECT 
   cmt_id, 
   thread_id, 
   cmt_text 
FROM comments 
WHERE cmt_id IN (
         SELECT 
            MIN(cmt_id) 
         FROM comments 
         GROUP BY thread_id);

SQL就是这样工作的,在您的查询中,它合并了行,而不是保留最小cmt_id的行,而是随机的行(或者最后一个,但我不指望它)。在上面的查询中,它将首先找到所有的min-id,然后获取每个min-id的扩展信息。

我不知道为什么MySQL允许您这样做,但通常在SQL中,下面的查询是无效的

SELECT cmt_id, thread_id, cmt_text, MIN(cmt_id) FROM comments 
GROUP BY thread_id;
如果SELECT列不可用,则不能指定该列

  • A
    分组依据
  • 聚合函数(例如,
    MIN
    MAX
    COUNT
    SUM
    …)
  • 在你的情况下,我会使用这个选择

    SELECT cmt_id, thread_id, cmt_text FROM comments INNER JOIN (
      SELECT MAX(cmt_id) AS cmt_id FROM comments 
      GROUP BY thread_id
    ) a ON a.cmt_id = comments.cmt_id 
    
    SELECT cmt_id, thread_id, cmt_text FROM comments INNER JOIN (
      SELECT MAX(cmt_id) AS cmt_id FROM comments 
      GROUP BY thread_id
    ) a ON a.cmt_id = comments.cmt_id