<small id='2qke6'></small><noframes id='2qke6'>

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

      1. <legend id='2qke6'><style id='2qke6'><dir id='2qke6'><q id='2qke6'></q></dir></style></legend>

        一起使用 ORDER BY 和 GROUP BY

        时间:2023-06-02
        <tfoot id='qxt5t'></tfoot>

            <tbody id='qxt5t'></tbody>

            1. <small id='qxt5t'></small><noframes id='qxt5t'>

                  <bdo id='qxt5t'></bdo><ul id='qxt5t'></ul>

                • <legend id='qxt5t'><style id='qxt5t'><dir id='qxt5t'><q id='qxt5t'></q></dir></style></legend>
                  <i id='qxt5t'><tr id='qxt5t'><dt id='qxt5t'><q id='qxt5t'><span id='qxt5t'><b id='qxt5t'><form id='qxt5t'><ins id='qxt5t'></ins><ul id='qxt5t'></ul><sub id='qxt5t'></sub></form><legend id='qxt5t'></legend><bdo id='qxt5t'><pre id='qxt5t'><center id='qxt5t'></center></pre></bdo></b><th id='qxt5t'></th></span></q></dt></tr></i><div id='qxt5t'><tfoot id='qxt5t'></tfoot><dl id='qxt5t'><fieldset id='qxt5t'></fieldset></dl></div>
                • 本文介绍了一起使用 ORDER BY 和 GROUP BY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的表看起来像这样(而且我使用的是 MySQL):

                  My table looks like this (and I'm using MySQL):

                  m_id | v_id | timestamp
                  ------------------------
                  6    |   1  | 1333635317
                  34   |   1  | 1333635323
                  34   |   1  | 1333635336
                  6    |   1  | 1333635343
                  6    |   1  | 1333635349
                  

                  我的目标是对每个 m_id 取一次,并按最高时间戳排序.

                  My target is to take each m_id one time, and order by the highest timestamp.

                  结果应该是:

                  m_id | v_id | timestamp
                  ------------------------
                  6    |   1  | 1333635349
                  34   |   1  | 1333635336
                  

                  我写了这个查询:

                  SELECT * FROM table GROUP BY m_id ORDER BY timestamp DESC
                  

                  但是,结果是:

                  m_id | v_id | timestamp
                  ------------------------
                  34   |   1  | 1333635323
                  6    |   1  | 1333635317
                  

                  我认为这是因为它首先执行 GROUP_BY 然后对结果进行排序.

                  I think it causes because it first does GROUP_BY and then ORDER the results.

                  有什么想法吗?谢谢.

                  推荐答案

                  一种正确使用 group by 的方法:

                  One way to do this that correctly uses group by:

                  select l.* 
                  from table l
                  inner join (
                    select 
                      m_id, max(timestamp) as latest 
                    from table 
                    group by m_id
                  ) r
                    on l.timestamp = r.latest and l.m_id = r.m_id
                  order by timestamp desc
                  

                  这是如何工作的:

                  • 为子查询中每个不同的m_id选择最新的时间戳
                  • 仅从 table 中选择与子查询中的行匹配的行(此操作 - 执行连接,但未从第二个表中选择列,它仅用作过滤器 -- 被称为 semijoin" 以防你好奇)
                  • 对行进行排序
                  • selects the latest timestamp for each distinct m_id in the subquery
                  • only selects rows from table that match a row from the subquery (this operation -- where a join is performed, but no columns are selected from the second table, it's just used as a filter -- is known as a "semijoin" in case you were curious)
                  • orders the rows

                  这篇关于一起使用 ORDER BY 和 GROUP BY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MySQL SELECT 最近几天? 下一篇:为一个值检查多列

                  相关文章

                    <tfoot id='tTUW7'></tfoot>

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

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

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