• <small id='jQ7yZ'></small><noframes id='jQ7yZ'>

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

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

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

      1. <tfoot id='jQ7yZ'></tfoot>
      2. MySQL groupwise MAX() 返回意外结果

        MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)

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

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

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

              <bdo id='GjmN2'></bdo><ul id='GjmN2'></ul>
                    <tbody id='GjmN2'></tbody>
                • 本文介绍了MySQL groupwise MAX() 返回意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  表格:贷款

                  Loan_no     Amount        SSS_no              Loan_date
                  
                  7           700.00        0104849222          2010-01-03 
                  8           200.00        0104849222          2010-02-28
                  9           300.00        0119611199          2010-11-18
                  10          150.00        3317131410          2012-11-28
                  11          600.00        0104849222          2011-01-03
                  14          175.00        3317131410          2012-12-05
                  15          260.00        3317131410          2013-02-08
                  16          230.00        0104849222          2013-03-06
                  17          265.00        0119611199          2011-04-30
                  18          455.00        3317131410          2013-03-10
                  

                  <小时>

                  预期结果:

                  我想检索最新的贷款由每个人(由他们的 SSS 号码识别).这结果应如下:

                  I would want to retrieve the latest loan availed off by each person (identified by their SSS number). The results should be as follows:

                  Loan_no           Amount                SSS_no                Loan_date
                  
                  16                230.00              0104849222              2013-03-06
                  17                265.00              0119611199              2011-04-30
                  18                455.00              3317131410              2013-03-10
                  

                  <小时>

                  使用的查询 # 1:

                  <代码>选择 *来自贷款GROUP BY SSS_noORDER BY Loan_date DESC

                  MYSQL 结果

                  Loan_no             Amount              SSS_no                  Loan_date
                  
                  10                  150.00            3317131410                2012-11-28
                  9                   300.00            0119611199                2010-11-18
                  7                   700.00            0104849222                2010-01-03
                  

                  <小时>

                  已使用的查询 # 2:

                  SELECT Loan_no, Amount, SSS_no, max(Loan_date)来自贷款GROUP BY SSS_no

                  MYSQL 结果

                  Loan_no            Amount                SSS_no                Loan_date
                  
                  7                  700.00                0104849222            2013-03-06
                  9                  300.00                0119611199            2011-04-30
                  10                 150.00                3317131410            2013-03-10
                  

                  有人可以帮我解决我的问题吗?谢谢.

                  Can anybody help me with my problem? Thanks.

                  推荐答案

                  MySQL 参考 提出了几种解决此问题的方法.最简单的是子查询:

                  The MySQL reference suggests several ways to solve this. The simplest is a subquery:

                  SELECT *
                  FROM   loan l1
                  WHERE  loan_date=(SELECT MAX(l2.loan_date)
                                FROM loan l2
                                WHERE l1.sss_no = l2.sss_no);
                  

                  鉴于这种类型的子查询可能性能不佳,他们还建议使用 JOIN(基本上是 Mahmoud Gamal 的回答):

                  Given that this type of subqueries potentially have bad performance, they also suggest using a JOIN (essentially Mahmoud Gamal's answer):

                  SELECT l1.loan_no, l1.amount, l1.sss_no, l1.loan_date
                  FROM loan l1
                  JOIN (
                    SELECT loan_no, MAX(loan_date) AS loan_date
                    FROM loan
                    GROUP BY sss_no) AS l2
                    ON l1.loan_date = l2.loan_date AND l1.sss_no = l2.sss_no;
                  

                  第三个选项是:

                  SELECT l1.loan_no, l1.amount, l1.sss_no, l1.loan_date
                  FROM loan l1
                  LEFT JOIN loan l2 ON l1.sss_no = l2.sss_no AND l1.loan_date < l2.loan_date
                  WHERE l2.sss_no IS NULL;
                  

                  LEFT JOIN 的工作原理是,当l1.loan_date 为最大值时,后面还有l2.loan_date,所以l2 行值将为 NULL.

                  The LEFT JOIN works on the basis that when l1.loan_date is at its maximum value, there is later l2.loan_date, so the l2 row values will be NULL.

                  所有这些都应该有相同的输出,但性能可能不同.

                  All these should have the same output, but likely differ in performance.

                  这篇关于MySQL groupwise MAX() 返回意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  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 行为不同)
                  MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)
                • <legend id='5xR5q'><style id='5xR5q'><dir id='5xR5q'><q id='5xR5q'></q></dir></style></legend><tfoot id='5xR5q'></tfoot>
                  • <bdo id='5xR5q'></bdo><ul id='5xR5q'></ul>
                      <tbody id='5xR5q'></tbody>

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

                      <small id='5xR5q'></small><noframes id='5xR5q'>