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

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

      <legend id='coD2d'><style id='coD2d'><dir id='coD2d'><q id='coD2d'></q></dir></style></legend>
      1. <tfoot id='coD2d'></tfoot>
          <bdo id='coD2d'></bdo><ul id='coD2d'></ul>
      2. 如何在 MySQL 中执行分组排名

        时间:2023-08-18

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

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

                <bdo id='w4Mkm'></bdo><ul id='w4Mkm'></ul>
              • <legend id='w4Mkm'><style id='w4Mkm'><dir id='w4Mkm'><q id='w4Mkm'></q></dir></style></legend>

                    <tbody id='w4Mkm'></tbody>
                  本文介绍了如何在 MySQL 中执行分组排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  所以我有一个表格如下:

                  So I have a table as follows:

                  ID_STUDENT | ID_CLASS | GRADE
                  -----------------------------
                     1       |    1     |  90
                     1       |    2     |  80
                     2       |    1     |  99
                     3       |    1     |  80
                     4       |    1     |  70
                     5       |    2     |  78
                     6       |    2     |  90
                     6       |    3     |  50
                     7       |    3     |  90
                  

                  然后我需要对它们进行分组、排序和排序:

                  I need to then group, sort and order them to give:

                  ID_STUDENT | ID_CLASS | GRADE | RANK
                  ------------------------------------
                      2      |    1     |  99   |  1
                      1      |    1     |  90   |  2
                      3      |    1     |  80   |  3
                      4      |    1     |  70   |  4
                      6      |    2     |  90   |  1
                      1      |    2     |  80   |  2
                      5      |    2     |  78   |  3
                      7      |    3     |  90   |  1
                      6      |    3     |  50   |  2
                  

                  现在我知道你可以使用临时变量来进行排名,像这里,但我如何为分组集做这件事?感谢您的任何见解!

                  Now I know that you can use a temp variable to rank, like here, but how do I do it for a grouped set? Thanks for any insight!

                  推荐答案

                  SELECT id_student, id_class, grade,
                     @student:=CASE WHEN @class <> id_class THEN 0 ELSE @student+1 END AS rn,
                     @class:=id_class AS clset
                  FROM
                    (SELECT @student:= -1) s,
                    (SELECT @class:= -1) c,
                    (SELECT *
                     FROM mytable
                     ORDER BY id_class, id_student
                    ) t
                  

                  这很简单:

                  1. 初始查询按 id_class 先排序,id_student 第二排序.
                  2. @student@class 被初始化为 -1
                  3. @class 用于测试是否输入了下一组.如果 id_class 的先前值(存储在 @class 中)不等于当前值(存储在 id_class 中),@student 被归零.否则递增.
                  4. @class 被赋值为 id_class 的新值,它将在下一行的第 3 步的测试中使用.
                  1. Initial query is ordered by id_class first, id_student second.
                  2. @student and @class are initialized to -1
                  3. @class is used to test if the next set is entered. If the previous value of the id_class (which is stored in @class) is not equal to the current value (which is stored in id_class), the @student is zeroed. Otherwise is is incremented.
                  4. @class is assigned with the new value of id_class, and it will be used in test on step 3 at the next row.

                  这篇关于如何在 MySQL 中执行分组排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 SELECT 插入 下一篇:防止 Node.js 中的 SQL 注入

                  相关文章

                  最新文章

                  • <bdo id='2iexX'></bdo><ul id='2iexX'></ul>

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

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