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

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

    1. <legend id='X17jr'><style id='X17jr'><dir id='X17jr'><q id='X17jr'></q></dir></style></legend>
    2. <tfoot id='X17jr'></tfoot>
        • <bdo id='X17jr'></bdo><ul id='X17jr'></ul>
      1. MySQL 在 ORDER BY 中获取行位置

        时间:2023-08-17

          <bdo id='azCfY'></bdo><ul id='azCfY'></ul>
        • <tfoot id='azCfY'></tfoot>

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

                  <tbody id='azCfY'></tbody>
              • <small id='azCfY'></small><noframes id='azCfY'>

                  本文介绍了MySQL 在 ORDER BY 中获取行位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  使用以下 MySQL 表:

                  With the following MySQL table:

                  +-----------------------------+
                  + id INT UNSIGNED             +
                  + name VARCHAR(100)           +
                  +-----------------------------+
                  

                  当按 name ASC 排序时,如何选择 单个 行及其在表中其他行中的位置.所以如果表数据看起来像这样,当按名称排序时:

                  How can I select a single row AND its position amongst the other rows in the table, when sorted by name ASC. So if the table data looks like this, when sorted by name:

                  +-----------------------------+
                  + id | name                   +
                  +-----------------------------+
                  +  5 | Alpha                  +
                  +  7 | Beta                   +
                  +  3 | Delta                  +
                  + .....                       +
                  +  1 | Zed                    +
                  +-----------------------------+
                  

                  如何选择 Beta 行以获取该行的当前位置?我正在寻找的结果集将是这样的:

                  How could I select the Beta row getting the current position of that row? The result set I'm looking for would be something like this:

                  +-----------------------------+
                  + id | position | name        +
                  +-----------------------------+
                  +  7 |        2 | Beta        +
                  +-----------------------------+
                  

                  我可以做一个简单的 SELECT * FROM tbl ORDER BY name ASC 然后在 PHP 中枚举行,但是只为单行加载一个可能很大的结果集似乎很浪费.

                  I can do a simple SELECT * FROM tbl ORDER BY name ASC then enumerate the rows in PHP, but it seems wasteful to load a potentially large resultset just for a single row.

                  推荐答案

                  使用这个:

                  SELECT x.id, 
                         x.position,
                         x.name
                    FROM (SELECT t.id,
                                 t.name,
                                 @rownum := @rownum + 1 AS position
                            FROM TABLE t
                            JOIN (SELECT @rownum := 0) r
                        ORDER BY t.name) x
                   WHERE x.name = 'Beta'
                  

                  ...获得唯一的位置值.这:

                  ...to get a unique position value. This:

                  SELECT t.id,
                         (SELECT COUNT(*)
                            FROM TABLE x
                           WHERE x.name <= t.name) AS position,
                         t.name    
                    FROM TABLE t      
                   WHERE t.name = 'Beta'
                  

                  ...会给关系相同的值.IE:如果有两个值排在第二位,当第一个查询将位置 2 给其中一个时,它们的位置都为 2,而另一个位置为 3...

                  ...will give ties the same value. IE: If there are two values at second place, they'll both have a position of 2 when the first query will give a position of 2 to one of them, and 3 to the other...

                  这篇关于MySQL 在 ORDER BY 中获取行位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何调试 MySQL 上超出的锁定等待超时? 下一篇:mysql 外键约束格式错误

                  相关文章

                  最新文章

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

                    <tfoot id='WtZ1N'></tfoot>
                      <bdo id='WtZ1N'></bdo><ul id='WtZ1N'></ul>

                  1. <legend id='WtZ1N'><style id='WtZ1N'><dir id='WtZ1N'><q id='WtZ1N'></q></dir></style></legend>
                    1. <small id='WtZ1N'></small><noframes id='WtZ1N'>