繁体   English   中英

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

[英]With MySQL, how can I generate a column containing the record index in a table?

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

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

我想对用户进行排名,以便我可以知道特定用户在哪里,即。 Joe 在 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) 

作为派生

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

有任何想法吗?

您可能想尝试以下操作:

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 @i:=@i+1 AS iterator, t.*
FROM tablename t,(SELECT @i:=0) foo

这是我使用的模板结构:

  select
          /*this is a row number counter*/
          ( select @rownum := @rownum + 1 from ( select @rownum := 0 ) d2 ) 
          as rownumber,
          d3.*
  from 
  ( select d1.* from table_name d1 ) d3

这是我的工作代码:

select     
           ( select @rownum := @rownum + 1 from ( select @rownum := 0 ) d2 ) 
           as rownumber,
           d3.*
from
(   select     year( d1.date ), month( d1.date ), count( d1.id )
    from       maindatabase d1
    where      ( ( d1.date >= '2013-01-01' ) and ( d1.date <= '2014-12-31' ) )
    group by   YEAR( d1.date ), MONTH( d1.date ) ) d3

你也可以使用

SELECT @curRow := ifnull(@curRow,0) + 1 Row, ...

初始化计数器变量。

如果您只想在按字段分数排序后了解某个特定用户的位置,您可以简单地从表中选择字段分数高于当前用户分数的所有行。 并使用返回的行号 + 1 来知道当前用户的位置。

假设你的表是league_girl并且你的主要字段是id ,你可以使用这个:

SELECT count(id) + 1 as rank from league_girl where score > <your_user_score>

假设 MySQL 支持它,您可以使用标准 SQL 子查询轻松完成此操作:

select 
    (count(*) from league_girl l1 where l2.score > l1.score and l1.id <> l2.id) as position,
    username,
    score
from league_girl l2
order by score;

对于大量显示的结果,这会有点慢,您需要切换到自联接。

我知道 OP 要求提供mysql答案,但由于我发现其他答案对我不起作用,

  • 他们中的大多数都因order by失败
  • 或者它们的效率非常低,并且使您的查询对于胖表非常缓慢

因此,为了节省像我这样的其他人的时间,只需在从数据库中检索它们后索引该行

PHP 中的示例:

$users = UserRepository::loadAllUsersAndSortByScore();

foreach($users as $index=>&$user){
    $user['rank'] = $index+1;
}

在 PHP 中使用偏移和限制进行分页的示例:

$limit = 20; //page size
$offset = 3; //page number

$users = UserRepository::loadAllUsersAndSortByScore();

foreach($users as $index=>&$user){
    $user['rank'] = $index+1+($limit*($offset-1));
}

我发现原始答案非常有用,但我也想根据我插入的行号获取一组特定的行。 因此,我将整个原始答案包装在一个子查询中,以便我可以引用我插入的行号。

SELECT * FROM 
( 
    SELECT *, @curRow := @curRow + 1 AS "row_number"
    FROM db.tableName, (SELECT @curRow := 0) r
) as temp
WHERE temp.row_number BETWEEN 1 and 10;

在子查询中使用子查询不是很有效,因此值得测试是否通过让 SQL 服务器处理此查询获得更好的结果,或者获取整个表并让应用程序/Web 服务器在事后操作行.

就个人而言,我的 SQL 服务器并不太忙,因此最好让它处理嵌套的子查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM