1. <legend id='xpd5m'><style id='xpd5m'><dir id='xpd5m'><q id='xpd5m'></q></dir></style></legend>
      <tfoot id='xpd5m'></tfoot>

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

      1. <small id='xpd5m'></small><noframes id='xpd5m'>

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

        时间:2023-08-20
      2. <tfoot id='BNfXE'></tfoot>

            • <small id='BNfXE'></small><noframes id='BNfXE'>

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

                  本文介绍了使用 MySQL,如何生成包含表中记录索引的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  Is there any way I can get the actual row number from a query?

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

                  I want to be able to order a table called league_girl by a field called score; and return the username and the actual row position of that username.

                  我想对用户进行排名,这样我就可以知道特定用户在哪里,即.Joe 在 200 个中排名第 100,即

                  I'm wanting to rank the users so i can tell where a particular user is, ie. Joe is position 100 out of 200, i.e.

                  User Score Row
                  Joe  100    1
                  Bob  50     2
                  Bill 10     3
                  

                  我在这里看到了一些解决方案,但我已经尝试了其中的大部分,但没有一个真正返回行号.

                  I've seen a few solutions on here but I've tried most of them and none of them actually return the row number.

                  我已经试过了:

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

                  作为衍生

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

                  ...but it doesn't seem to return the row position.

                  有什么想法吗?

                  推荐答案

                  您可能想尝试以下操作:

                  You may want to try the following:

                  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 命令.

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

                  测试用例:

                  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)
                  

                  这篇关于使用 MySQL,如何生成包含表中记录索引的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MySQL - 在选择时获取行号 下一篇:在 MySQL 中选择除一列之外的所有列?

                  相关文章

                  最新文章

                  1. <small id='6uxMA'></small><noframes id='6uxMA'>

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