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

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

      <small id='1i8UD'></small><noframes id='1i8UD'>

        <bdo id='1i8UD'></bdo><ul id='1i8UD'></ul>

    2. 使用 MAX 和 GROUP BY 选择所有相应的字段

      时间:2023-06-25

          <tbody id='qs25I'></tbody>
          <legend id='qs25I'><style id='qs25I'><dir id='qs25I'><q id='qs25I'></q></dir></style></legend>

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

              1. <tfoot id='qs25I'></tfoot>

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

                本文介绍了使用 MAX 和 GROUP BY 选择所有相应的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有这张桌子:

                我想发出一个请求,该请求将为每个 deal_id 返回具有最高 timestamp 和相应 status_id.

                And I would like to make a request that would return for each deal_id the row with the highest timestamp, and the corresponding status_id.

                所以对于这个例子,我会返回 2 行:

                So for this example, I would have returned 2 rows :

                1226, 3, 2009-08-18 12:10:25
                1227, 2, 2009-08-17 14:31:25
                

                我试着用这个查询来做

                SELECT deal_id, status_id, max(timestamp) FROM deal_status GROUP BY deal_id
                

                但它会返回错误的status_id :

                1226, 1, 2009-08-18 12:10:25
                1227, 1, 2009-08-17 14:31:25
                

                推荐答案

                如果没有一个主键字段,我认为最好的选择是:

                without a single primary key field, I think your best bet is:

                select * from deal_status
                inner join
                  (select deal_id as did, max(timestamp) as ts
                  from deal_status group by deal_id) as ds
                  on deal_status.deal_id = ds.did and deal_status.timestamp = ds.ts
                

                如果您允许同一产品同时具有两种不同的状态,这仍然不起作用

                this still won't work if you allow having two different statuses for the same product at the same time

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

                  <tbody id='dzB6T'></tbody>

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

                    <legend id='dzB6T'><style id='dzB6T'><dir id='dzB6T'><q id='dzB6T'></q></dir></style></legend>

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

                  2. <tfoot id='dzB6T'></tfoot>

                      • <bdo id='dzB6T'></bdo><ul id='dzB6T'></ul>