开发者社区> 问答> 正文

使用MySQL,如何生成包含表中记录索引的列?

有什么办法可以从查询中获取实际的行号?

我希望能够通过一个名为score的字段来订购一个名为League_girl的表;并返回用户名和该用户名的实际行位置。

我想对用户进行排名,以便我可以告诉特定用户在哪里。乔在200中排名100,即

User Score Row Joe 100 1 Bob 50 2 Bill 10 3 我在这里看到了一些解决方案,但是我已经尝试了大多数解决方案,但实际上没有一个返回行号。

我已经试过了:

SELECT position, username, score FROM (SELECT @row := @row + 1 AS position, username, score FROM league_girl GROUP BY username ORDER BY score DESC) 作为派生

...但是它似乎没有返回行位置。

有任何想法吗?

展开
收起
保持可爱mmm 2020-05-10 17:13:58 439 0
1 条回答
写回答
取消 提交回答
  • 您可能需要尝试以下方法:

    SELECT l.position, l.username, l.score, @curRow := @curRow + 1 AS row_number FROM league_girl l JOIN (SELECT @curRow := 0) r; 该JOIN (SELECT @curRow := 0)部分允许变量初始化,而无需单独的SET命令。

    测试用例:

    CREATE TABLE league_girl (position int, username varchar(10), score int); INSERT INTO league_girl VALUES (1, 'a', 10); INSERT INTO league_girl VALUES (2, 'b', 25); INSERT INTO league_girl VALUES (3, 'c', 75); INSERT INTO league_girl VALUES (4, 'd', 25); INSERT INTO league_girl VALUES (5, 'e', 55); INSERT INTO league_girl VALUES (6, 'f', 80); INSERT INTO league_girl VALUES (7, 'g', 15); 测试查询:

    SELECT l.position, l.username, l.score, @curRow := @curRow + 1 AS row_number FROM league_girl l JOIN (SELECT @curRow := 0) r WHERE l.score > 50; 结果:

    +----------+----------+-------+------------+ | position | username | score | row_number | +----------+----------+-------+------------+ | 3 | c | 75 | 1 | | 5 | e | 55 | 2 | | 6 | f | 80 | 3 | +----------+----------+-------+------------+ 3 rows in set (0.00 sec)您可能需要尝试以下方法:

    SELECT l.position, l.username, l.score, @curRow := @curRow + 1 AS row_number FROM league_girl l JOIN (SELECT @curRow := 0) r; 该JOIN (SELECT @curRow := 0)部分允许变量初始化,而无需单独的SET命令。

    测试用例:

    CREATE TABLE league_girl (position int, username varchar(10), score int); INSERT INTO league_girl VALUES (1, 'a', 10); INSERT INTO league_girl VALUES (2, 'b', 25); INSERT INTO league_girl VALUES (3, 'c', 75); INSERT INTO league_girl VALUES (4, 'd', 25); INSERT INTO league_girl VALUES (5, 'e', 55); INSERT INTO league_girl VALUES (6, 'f', 80); INSERT INTO league_girl VALUES (7, 'g', 15); 测试查询:

    SELECT l.position, l.username, l.score, @curRow := @curRow + 1 AS row_number FROM league_girl l JOIN (SELECT @curRow := 0) r WHERE l.score > 50; 结果:

    +----------+----------+-------+------------+ | position | username | score | row_number | +----------+----------+-------+------------+ | 3 | c | 75 | 1 | | 5 | e | 55 | 2 | | 6 | f | 80 | 3 | +----------+----------+-------+------------+ 3 rows in set (0.00 sec)您可能需要尝试以下方法:

    SELECT l.position, l.username, l.score, @curRow := @curRow + 1 AS row_number FROM league_girl l JOIN (SELECT @curRow := 0) r; 该JOIN (SELECT @curRow := 0)部分允许变量初始化,而无需单独的SET命令。

    测试用例:

    CREATE TABLE league_girl (position int, username varchar(10), score int); INSERT INTO league_girl VALUES (1, 'a', 10); INSERT INTO league_girl VALUES (2, 'b', 25); INSERT INTO league_girl VALUES (3, 'c', 75); INSERT INTO league_girl VALUES (4, 'd', 25); INSERT INTO league_girl VALUES (5, 'e', 55); INSERT INTO league_girl VALUES (6, 'f', 80); INSERT INTO league_girl VALUES (7, 'g', 15); 测试查询:

    SELECT l.position, l.username, l.score, @curRow := @curRow + 1 AS row_number FROM league_girl l JOIN (SELECT @curRow := 0) r WHERE l.score > 50; 结果:

    +----------+----------+-------+------------+ | position | username | score | row_number | +----------+----------+-------+------------+ | 3 | c | 75 | 1 | | 5 | e | 55 | 2 | | 6 | f | 80 | 3 | +----------+----------+-------+------------+ 3 rows in set (0.00 sec)来源:stack overflow

    2020-05-10 17:14:42
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
One Box: 解读事务与分析一体化数据库 HybridDB for MySQL 立即下载
One Box:解读事务与分析一体化数据库HybridDB for MySQL 立即下载
如何支撑HTAP场景-HybridDB for MySQL系统架构和技术演进 立即下载

相关镜像