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

      1. <tfoot id='Czhi6'></tfoot>
      2. <i id='Czhi6'><tr id='Czhi6'><dt id='Czhi6'><q id='Czhi6'><span id='Czhi6'><b id='Czhi6'><form id='Czhi6'><ins id='Czhi6'></ins><ul id='Czhi6'></ul><sub id='Czhi6'></sub></form><legend id='Czhi6'></legend><bdo id='Czhi6'><pre id='Czhi6'><center id='Czhi6'></center></pre></bdo></b><th id='Czhi6'></th></span></q></dt></tr></i><div id='Czhi6'><tfoot id='Czhi6'></tfoot><dl id='Czhi6'><fieldset id='Czhi6'></fieldset></dl></div>
        • <bdo id='Czhi6'></bdo><ul id='Czhi6'></ul>
        <legend id='Czhi6'><style id='Czhi6'><dir id='Czhi6'><q id='Czhi6'></q></dir></style></legend>
      3. MySQL中的排名函数

        时间:2023-08-19

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

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

              1. <legend id='mGOCW'><style id='mGOCW'><dir id='mGOCW'><q id='mGOCW'></q></dir></style></legend>
                • 本文介绍了MySQL中的排名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要找出客户的等级.在这里,我根据我的要求添加了相应的 ANSI 标准 SQL 查询.请帮我把它转换成 MySQL.

                  I need to find out rank of customers. Here I am adding the corresponding ANSI standard SQL query for my requirement. Please help me to convert it to MySQL .

                  SELECT RANK() OVER (PARTITION BY Gender ORDER BY Age) AS [Partition by Gender], 
                    FirstName, 
                    Age,
                    Gender 
                  FROM Person
                  

                  MySQL 中是否有查询排名的函数?

                  Is there any function to find out rank in MySQL?

                  推荐答案

                  一种选择是使用排名变量,例如:

                  One option is to use a ranking variable, such as the following:

                  SELECT    first_name,
                            age,
                            gender,
                            @curRank := @curRank + 1 AS rank
                  FROM      person p, (SELECT @curRank := 0) r
                  ORDER BY  age;
                  

                  (SELECT @curRank := 0) 部分允许变量初始化,而无需单独的 SET 命令.

                  The (SELECT @curRank := 0) part allows the variable initialization without requiring a separate SET command.

                  测试用例:

                  CREATE TABLE person (id int, first_name varchar(20), age int, gender char(1));
                  
                  INSERT INTO person VALUES (1, 'Bob', 25, 'M');
                  INSERT INTO person VALUES (2, 'Jane', 20, 'F');
                  INSERT INTO person VALUES (3, 'Jack', 30, 'M');
                  INSERT INTO person VALUES (4, 'Bill', 32, 'M');
                  INSERT INTO person VALUES (5, 'Nick', 22, 'M');
                  INSERT INTO person VALUES (6, 'Kathy', 18, 'F');
                  INSERT INTO person VALUES (7, 'Steve', 36, 'M');
                  INSERT INTO person VALUES (8, 'Anne', 25, 'F');
                  

                  结果:

                  +------------+------+--------+------+
                  | first_name | age  | gender | rank |
                  +------------+------+--------+------+
                  | Kathy      |   18 | F      |    1 |
                  | Jane       |   20 | F      |    2 |
                  | Nick       |   22 | M      |    3 |
                  | Bob        |   25 | M      |    4 |
                  | Anne       |   25 | F      |    5 |
                  | Jack       |   30 | M      |    6 |
                  | Bill       |   32 | M      |    7 |
                  | Steve      |   36 | M      |    8 |
                  +------------+------+--------+------+
                  8 rows in set (0.02 sec)
                  

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

                  上一篇:如何在 MySQL 中进行正则表达式替换? 下一篇:在 MySql 中执行查询时与 only_full_group_by 相关的错

                  相关文章

                  最新文章

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

                  <tfoot id='Ya6rJ'></tfoot>

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