zoukankan      html  css  js  c++  java
  • 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] >

  • 相关阅读:
    初解DLL基本知识
    读《暗时间》笔记
    红队 Cobalt Strike 安全加固
    《天书夜读 —— 从汇编语言到Windows内核编程》3.3 汇编反C语言练习 参考答案
    记录一次从WordPress后台到拿下主机shell的渗透
    今日记 2019.6.19
    解决Termux无法通过metasploit.sh安装Metasploit的问题
    解决ajax中文乱码问题
    jsp开发知识
    第一个Java web项目:员工管理系统
  • 原文地址:https://www.cnblogs.com/smallfishy/p/12916944.html
Copyright © 2011-2022 走看看