1. <legend id='2YC7a'><style id='2YC7a'><dir id='2YC7a'><q id='2YC7a'></q></dir></style></legend>
        <tfoot id='2YC7a'></tfoot>
          <bdo id='2YC7a'></bdo><ul id='2YC7a'></ul>

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

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

        在 MySQL 中使用 GROUP BY 选择最近的行

        Select most recent row with GROUP BY in MySQL(在 MySQL 中使用 GROUP BY 选择最近的行)
            • <legend id='vHwa6'><style id='vHwa6'><dir id='vHwa6'><q id='vHwa6'></q></dir></style></legend>
                <tbody id='vHwa6'></tbody>

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

                • <bdo id='vHwa6'></bdo><ul id='vHwa6'></ul>
                • <small id='vHwa6'></small><noframes id='vHwa6'>

                  <tfoot id='vHwa6'></tfoot>

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

                  问题描述

                  我正在尝试选择每个用户最近一次付款.我现在的查询选择用户第一次付款.IE.如果用户进行了两次付款并且 payment.ids 是 10 和 11,则查询将选择具有付款 ID 信息的用户,而不是 11.

                  I'm trying to select each user with their most recent payment. The query I have now selects the users first payment. I.e. if a user has made two payments and the payment.ids are 10 and 11, the query selects the user with the info for payment id 10, not 11.

                    SELECT users.*, payments.method, payments.id AS payment_id 
                      FROM `users` 
                           LEFT JOIN `payments` ON users.id = payments.user_id 
                  GROUP BY users.id
                  

                  我添加了 ORDER BY payment.id,但查询似乎忽略了它,仍然选择第一笔付款.

                  I've added ORDER BY payments.id, but the query seems to ignore it and still selects the first payment.

                  感谢所有帮助.谢谢.

                  推荐答案

                  您想要 分组最大值;本质上,将支付表分组以识别最大记录,然后将结果与自身连接以获取其他列:

                  You want the groupwise maximum; in essence, group the payments table to identify the maximal records, then join the result back with itself to fetch the other columns:

                  SELECT users.*, payments.method, payments.id AS payment_id
                  FROM   payments NATURAL JOIN (
                    SELECT   user_id, MAX(id) AS id 
                    FROM     payments
                    GROUP BY user_id
                  ) t RIGHT JOIN users ON users.id = t.user_id
                  

                  请注意,MAX(id) 可能不是最近的付款",具体取决于您的应用程序和架构:通常最好确定最最近"基于 TIMESTAMP 而不是基于合成标识符,例如 AUTO_INCREMENT 主键列.

                  Note that MAX(id) may not be the "most recent payment", depending on your application and schema: it's usually better to determine "most recent" based off TIMESTAMP than based off synthetic identifiers such as an AUTO_INCREMENT primary key column.

                  这篇关于在 MySQL 中使用 GROUP BY 选择最近的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 查询中包含缺失的月份)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)

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

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