• <legend id='Y4OU8'><style id='Y4OU8'><dir id='Y4OU8'><q id='Y4OU8'></q></dir></style></legend>
    • <bdo id='Y4OU8'></bdo><ul id='Y4OU8'></ul>
    <tfoot id='Y4OU8'></tfoot>

        <small id='Y4OU8'></small><noframes id='Y4OU8'>

        <i id='Y4OU8'><tr id='Y4OU8'><dt id='Y4OU8'><q id='Y4OU8'><span id='Y4OU8'><b id='Y4OU8'><form id='Y4OU8'><ins id='Y4OU8'></ins><ul id='Y4OU8'></ul><sub id='Y4OU8'></sub></form><legend id='Y4OU8'></legend><bdo id='Y4OU8'><pre id='Y4OU8'><center id='Y4OU8'></center></pre></bdo></b><th id='Y4OU8'></th></span></q></dt></tr></i><div id='Y4OU8'><tfoot id='Y4OU8'></tfoot><dl id='Y4OU8'><fieldset id='Y4OU8'></fieldset></dl></div>

        如何使用GROUP BY获取每个组中的最新记录?

        How to get the latest record in each group using GROUP BY?(如何使用GROUP BY获取每个组中的最新记录?)
            <tbody id='AfZb2'></tbody>

              <legend id='AfZb2'><style id='AfZb2'><dir id='AfZb2'><q id='AfZb2'></q></dir></style></legend>
              • <bdo id='AfZb2'></bdo><ul id='AfZb2'></ul>
              • <tfoot id='AfZb2'></tfoot>

                  <small id='AfZb2'></small><noframes id='AfZb2'>

                  <i id='AfZb2'><tr id='AfZb2'><dt id='AfZb2'><q id='AfZb2'><span id='AfZb2'><b id='AfZb2'><form id='AfZb2'><ins id='AfZb2'></ins><ul id='AfZb2'></ul><sub id='AfZb2'></sub></form><legend id='AfZb2'></legend><bdo id='AfZb2'><pre id='AfZb2'><center id='AfZb2'></center></pre></bdo></b><th id='AfZb2'></th></span></q></dt></tr></i><div id='AfZb2'><tfoot id='AfZb2'></tfoot><dl id='AfZb2'><fieldset id='AfZb2'></fieldset></dl></div>

                  本文介绍了如何使用GROUP BY获取每个组中的最新记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  假设我有一个名为 messages 的表,其中包含列:

                  Let's say I have a table called messages with the columns:

                  id | from_id | to_id | subject | message | timestamp
                  

                  我只想从每个用户那里获取最新消息,就像您在深入了解实际线程之前在 Facebook 收件箱中看到的一样.

                  I want to get the latest message from each user only, like you would see in your FaceBook inbox before you drill down into the actual thread.

                  这个查询似乎让我接近我需要的结果:

                  This query seems to get me close to the result I need:

                  SELECT * FROM messages GROUP BY from_id
                  

                  然而,查询给我的是来自每个用户的最旧的消息,而不是最新的消息.

                  However the query is giving me the oldest message from each user and not the newest.

                  我无法弄清楚这一点.

                  推荐答案

                  你应该找出每组(子查询)中最后一个timestamp的值,然后把这个子查询加入到表中 -

                  You should find out last timestamp values in each group (subquery), and then join this subquery to the table -

                  SELECT t1.* FROM messages t1
                    JOIN (SELECT from_id, MAX(timestamp) timestamp FROM messages GROUP BY from_id) t2
                      ON t1.from_id = t2.from_id AND t1.timestamp = t2.timestamp;
                  

                  这篇关于如何使用GROUP BY获取每个组中的最新记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)

                      <tbody id='yVFSQ'></tbody>

                    <small id='yVFSQ'></small><noframes id='yVFSQ'>

                  • <legend id='yVFSQ'><style id='yVFSQ'><dir id='yVFSQ'><q id='yVFSQ'></q></dir></style></legend>

                          <bdo id='yVFSQ'></bdo><ul id='yVFSQ'></ul>
                          <tfoot id='yVFSQ'></tfoot>
                            <i id='yVFSQ'><tr id='yVFSQ'><dt id='yVFSQ'><q id='yVFSQ'><span id='yVFSQ'><b id='yVFSQ'><form id='yVFSQ'><ins id='yVFSQ'></ins><ul id='yVFSQ'></ul><sub id='yVFSQ'></sub></form><legend id='yVFSQ'></legend><bdo id='yVFSQ'><pre id='yVFSQ'><center id='yVFSQ'></center></pre></bdo></b><th id='yVFSQ'></th></span></q></dt></tr></i><div id='yVFSQ'><tfoot id='yVFSQ'></tfoot><dl id='yVFSQ'><fieldset id='yVFSQ'></fieldset></dl></div>