• mysql-rank排名问题(添加行号)


    create table score (id int primary key, score int , orders int );
    insert into score values(5,20);
    SET @prev_value = NULL;
    SET @rank_count = 0;
    SELECT id, score, CASE
    WHEN @prev_value = score THEN @rank_count
    WHEN @prev_value := score THEN @rank_count := @rank_count + 1
    END AS rank
    FROM score
    ORDER BY score [desc];

    -----------------------------------------
    --实例

    [mysql - root@localhost@testmysql.sock 14:10:42] >create table score (id int primary key, score int );
    Query OK, 0 rows affected (0.01 sec)

    [mysql - root@localhost@testmysql.sock 14:10:46] >show create table scoreG;
    *************************** 1. row ***************************
    Table: score
    Create Table: CREATE TABLE `score` (
    `id` int(11) NOT NULL,
    `score` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    1 row in set (0.00 sec)

    ERROR:
    No query specified

    [mysql - root@localhost@testmysql.sock 14:10:51] >insert into score values(1,20);
    Query OK, 1 row affected (0.01 sec)

    [mysql - root@localhost@testmysql.sock 14:11:10] >insert into score values(2,30);
    Query OK, 1 row affected (0.00 sec)

    [mysql - root@localhost@testmysql.sock 14:11:14] >insert into score values(3,30);
    Query OK, 1 row affected (0.00 sec)

    [mysql - root@localhost@testmysql.sock 14:11:18] >insert into score values(4,40);
    Query OK, 1 row affected (0.01 sec)

    [mysql - root@localhost@testmysql.sock 14:11:22] >insert into score values(5,10);
    Query OK, 1 row affected (0.01 sec)

    [mysql - root@localhost@testmysql.sock 14:11:26] >insert into score values(6,90);
    Query OK, 1 row affected (0.00 sec)

    [mysql - root@localhost@testmysql.sock 14:11:37] >insert into score values(7,20);
    Query OK, 1 row affected (0.01 sec)

    [mysql - root@localhost@testmysql.sock 14:13:05] >SET @prev_value = NULL;
    Query OK, 0 rows affected (0.00 sec)

    [mysql - root@localhost@testmysql.sock 14:13:23] >SET @rank_count = 0;
    Query OK, 0 rows affected (0.00 sec)

    [mysql - root@localhost@testmysql.sock 14:13:23] >SELECT id, score, CASE
    -> WHEN @prev_value = score THEN @rank_count
    -> WHEN @prev_value := score THEN @rank_count := @rank_count + 1
    -> END AS rank
    -> FROM score
    -> ORDER BY score;
    +----+-------+------+
    | id | score | rank |
    +----+-------+------+
    | 5 | 10 | 1 |
    | 1 | 20 | 2 |
    | 7 | 20 | 2 |
    | 2 | 30 | 3 |
    | 3 | 30 | 3 |
    | 4 | 40 | 4 |
    | 6 | 90 | 5 |
    +----+-------+------+
    7 rows in set (0.00 sec)

    [mysql - root@localhost@testmysql.sock 14:13:23] >SET @prev_value = NULL;
    Query OK, 0 rows affected (0.00 sec)

    [mysql - root@localhost@testmysql.sock 14:13:35] >SET @rank_count = 0;
    Query OK, 0 rows affected (0.00 sec)

    [mysql - root@localhost@testmysql.sock 14:13:35] >SELECT id, score, CASE
    -> WHEN @prev_value = score THEN @rank_count
    -> WHEN @prev_value := score THEN @rank_count := @rank_count + 1
    -> END AS rank
    -> FROM score
    -> ORDER BY score desc;
    +----+-------+------+
    | id | score | rank |
    +----+-------+------+
    | 6 | 90 | 1 |
    | 4 | 40 | 2 |
    | 2 | 30 | 3 |
    | 3 | 30 | 3 |
    | 1 | 20 | 4 |
    | 7 | 20 | 4 |
    | 5 | 10 | 5 |
    +----+-------+------+
    7 rows in set (0.00 sec)

    [mysql - root@localhost@testmysql.sock 14:13:36] >

  • 相关阅读:
    Web 应用程序中的安全向量 – ASP.NET MVC 4 系列
    成员资格、授权 – ASP.NET MVC 4 系列
    数据注解和验证 – ASP.NET MVC 4 系列
    表单和 HTML 辅助方法– ASP.NET MVC 4 系列
    模型(Model)– ASP.NET MVC 4 系列
    Razor 视图引擎 – ASP.NET MVC 4 系列
    视图(View) – ASP.NET MVC 4 系列
    控制器(Controller) – ASP.NET MVC 4 系列
    简介 – ASP.NET MVC 4 系列
    一般处理程序处理图片(动态给图片加上水印、保存缩略图、验证码)
  • 原文地址:https://www.cnblogs.com/smallfishy/p/12916944.html
Copyright © 2020-2023  润新知