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

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

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

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

      在 GROUP BY 中使用 LIMIT 以获得每组 N 个结果?

      时间:2023-08-19

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

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

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

              <tfoot id='pAyw2'></tfoot>

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

                本文介绍了在 GROUP BY 中使用 LIMIT 以获得每组 N 个结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                以下查询:

                SELECT
                year, id, rate
                FROM h
                WHERE year BETWEEN 2000 AND 2009
                AND id IN (SELECT rid FROM table2)
                GROUP BY id, year
                ORDER BY id, rate DESC
                

                产量:

                year    id  rate
                2006    p01 8
                2003    p01 7.4
                2008    p01 6.8
                2001    p01 5.9
                2007    p01 5.3
                2009    p01 4.4
                2002    p01 3.9
                2004    p01 3.5
                2005    p01 2.1
                2000    p01 0.8
                2001    p02 12.5
                2004    p02 12.4
                2002    p02 12.2
                2003    p02 10.3
                2000    p02 8.7
                2006    p02 4.6
                2007    p02 3.3
                

                我只想要每个 id 的前 5 个结果:

                What I'd like is only the top 5 results for each id:

                2006    p01 8
                2003    p01 7.4
                2008    p01 6.8
                2001    p01 5.9
                2007    p01 5.3
                2001    p02 12.5
                2004    p02 12.4
                2002    p02 12.2
                2003    p02 10.3
                2000    p02 8.7
                

                有没有办法使用某种在 GROUP BY 中起作用的类似 LIMIT 的修饰符来做到这一点?

                Is there a way to do this using some kind of LIMIT like modifier that works within the GROUP BY?

                推荐答案

                你可以使用 GROUP_CONCAT 聚合函数将所有年份放入一个列中,按 id 分组并按 rate 排序:

                You could use GROUP_CONCAT aggregated function to get all years into a single column, grouped by id and ordered by rate:

                SELECT   id, GROUP_CONCAT(year ORDER BY rate DESC) grouped_year
                FROM     yourtable
                GROUP BY id
                

                结果:

                -----------------------------------------------------------
                |  ID | GROUPED_YEAR                                      |
                -----------------------------------------------------------
                | p01 | 2006,2003,2008,2001,2007,2009,2002,2004,2005,2000 |
                | p02 | 2001,2004,2002,2003,2000,2006,2007                |
                -----------------------------------------------------------
                

                然后你可以使用 FIND_IN_SET,返回第一个参数在第二个参数中的位置,例如.

                And then you could use FIND_IN_SET, that returns the position of the first argument inside the second one, eg.

                SELECT FIND_IN_SET('2006', '2006,2003,2008,2001,2007,2009,2002,2004,2005,2000');
                1
                
                SELECT FIND_IN_SET('2009', '2006,2003,2008,2001,2007,2009,2002,2004,2005,2000');
                6
                

                使用 GROUP_CONCATFIND_IN_SET 的组合,并通过 find_in_set 返回的位置进行过滤,然后您可以使用此查询,该查询仅返回每个 ID 的前 5 年:

                Using a combination of GROUP_CONCAT and FIND_IN_SET, and filtering by the position returned by find_in_set, you could then use this query that returns only the first 5 years for every id:

                SELECT
                  yourtable.*
                FROM
                  yourtable INNER JOIN (
                    SELECT
                      id,
                      GROUP_CONCAT(year ORDER BY rate DESC) grouped_year
                    FROM
                      yourtable
                    GROUP BY id) group_max
                  ON yourtable.id = group_max.id
                     AND FIND_IN_SET(year, grouped_year) BETWEEN 1 AND 5
                ORDER BY
                  yourtable.id, yourtable.year DESC;
                

                请参阅 fiddle 此处.

                Please see fiddle here.

                请注意,如果多于一行可以具有相同的费率,您应该考虑在费率列而不是年份列上使用 GROUP_CONCAT(DISTINCT rate ORDER BY rate).

                Please note that if more than one row can have the same rate, you should consider using GROUP_CONCAT(DISTINCT rate ORDER BY rate) on the rate column instead of the year column.

                GROUP_CONCAT 返回的字符串的最大长度是有限的,因此如果您需要为每个组选择几条记录,这很有效.

                The maximum length of the string returned by GROUP_CONCAT is limited, so this works well if you need to select a few records for every group.

                这篇关于在 GROUP BY 中使用 LIMIT 以获得每组 N 个结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:INNER JOIN、LEFT JOIN、RIGHT JOIN 和 FULL JOIN 有什么区别 下一篇:MySQL - 行到列

                相关文章

                最新文章

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

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

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