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

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

      mysql选择前n个最大值

      时间:2023-05-23
          <tbody id='UevBY'></tbody>
        <legend id='UevBY'><style id='UevBY'><dir id='UevBY'><q id='UevBY'></q></dir></style></legend>
      • <tfoot id='UevBY'></tfoot>

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

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

              <i id='UevBY'><tr id='UevBY'><dt id='UevBY'><q id='UevBY'><span id='UevBY'><b id='UevBY'><form id='UevBY'><ins id='UevBY'></ins><ul id='UevBY'></ul><sub id='UevBY'></sub></form><legend id='UevBY'></legend><bdo id='UevBY'><pre id='UevBY'><center id='UevBY'></center></pre></bdo></b><th id='UevBY'></th></span></q></dt></tr></i><div id='UevBY'><tfoot id='UevBY'></tfoot><dl id='UevBY'><fieldset id='UevBY'></fieldset></dl></div>
                本文介绍了mysql选择前n个最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                如何从表格中选择前 n 个最大值?

                How can you select the top n max values from a table?

                对于这样的表格:

                column1  column2
                   1       foo
                   2       foo
                   3       foo
                   4       foo
                   5       bar
                   6       bar
                   7       bar
                   8       bar
                

                对于 n=2,结果需要是:

                For n=2, the result needs to be:

                3    
                4    
                7    
                8    
                

                下面的方法只选择每个组的最大值.

                The approach below selects only the max value for each group.

                SELECT max(column1) FROM table GROUP BY column2
                

                返回:

                4
                8
                

                推荐答案

                对于 n=2 你可以

                SELECT max(column1) m 
                FROM table t
                GROUP BY column2
                UNION
                SELECT max(column1) m
                FROM table t
                WHERE column1 NOT IN (SELECT max(column1) 
                                      WHERE column2 = t.column2)
                

                对于任何 n,您都可以使用 此处所述的方法 模拟分区上的排名.

                for any n you could use approaches described here to simulate rank over partition.

                实际上,这篇文章将为您提供所需的一切.

                Actually this article will give you exactly what you need.

                基本上是这样的

                SELECT t.*
                FROM
                   (SELECT grouper,
                          (SELECT val 
                           FROM table li
                           WHERE li.grouper = dlo.grouper
                           ORDER BY
                                 li.grouper, li.val DESC
                           LIMIT 2,1) AS mid
                   FROM 
                      (
                      SELECT DISTINCT grouper
                      FROM table
                      ) dlo 
                   ) lo, table t
                WHERE t.grouper = lo.grouper
                      AND t.val > lo.mid
                

                grouper 替换为要分组的列的名称,将 val 替换为包含值的列的名称.

                Replace grouper with the name of the column you want to group by and val with the name of the column that hold the values.

                从最内部的查询开始逐步确定它的功能并运行它们.

                To work out how exactly it functions go step-by-step from the most inner query and run them.

                此外,还有一个轻微的简化 - 如果某些类别没有足够的值,则找到 mid 的子查询可以返回 NULL,因此应该将其与某个常量合并,以便在比较(在您的情况下,它将是 val 域的 MIN,在文章中它是 MAX).

                Also, there is a slight simplification - the subquery that finds the mid can return NULL if certain category does not have enough values so there should be COALESCE of that to some constant that would make sense in the comparison (in your case it would be MIN of domain of the val, in article it is MAX).

                我忘了说,决定 n (LIMIT n,1) 的是 LIMIT 2,1.

                I forgot to mention that it is the LIMIT 2,1 that determines the n (LIMIT n,1).

                这篇关于mysql选择前n个最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:仅使用 MySQL 查询删除重复项? 下一篇:MySQL自动将字符串转换/转换为数字?

                相关文章

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

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

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