<small id='woarX'></small><noframes id='woarX'>

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

        MySQL - 在选择时获取行号

        时间:2023-08-20
      1. <small id='duSCI'></small><noframes id='duSCI'>

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

              • <tfoot id='duSCI'></tfoot>
                  <tbody id='duSCI'></tbody>
                  本文介绍了MySQL - 在选择时获取行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如果项目已排序,我可以运行 select 语句并获取行号吗?

                  Can I run a select statement and get the row number if the items are sorted?

                  我有一张这样的桌子:

                  mysql> describe orders;
                  +-------------+---------------------+------+-----+---------+----------------+
                  | Field       | Type                | Null | Key | Default | Extra          |
                  +-------------+---------------------+------+-----+---------+----------------+
                  | orderID     | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
                  | itemID      | bigint(20) unsigned | NO   |     | NULL    |                |
                  +-------------+---------------------+------+-----+---------+----------------+
                  

                  然后我可以运行此查询以按 ID 获取订单数:

                  I can then run this query to get the number of orders by ID:

                  SELECT itemID, COUNT(*) as ordercount
                  FROM orders
                  GROUP BY itemID ORDER BY ordercount DESC;
                  

                  这给了我表格中每个 itemID 的计数,如下所示:

                  This gives me a count of each itemID in the table like this:

                  +--------+------------+
                  | itemID | ordercount |
                  +--------+------------+
                  |    388 |          3 |
                  |    234 |          2 |
                  |   3432 |          1 |
                  |    693 |          1 |
                  |   3459 |          1 |
                  +--------+------------+
                  

                  我也想得到行号,所以我可以知道 itemID=388 是第一行,234 是第二行,等等(本质上是订单,而不仅仅是原始计数).我知道当我得到结果集时我可以在 Java 中执行此操作,但我想知道是否有一种方法可以完全在 SQL 中处理它.

                  I want to get the row number as well, so I could tell that itemID=388 is the first row, 234 is second, etc (essentially the ranking of the orders, not just a raw count). I know I can do this in Java when I get the result set back, but I was wondering if there was a way to handle it purely in SQL.

                  更新

                  设置等级会将其添加到结果集中,但未正确排序:

                  Setting the rank adds it to the result set, but not properly ordered:

                  mysql> SET @rank=0;
                  Query OK, 0 rows affected (0.00 sec)
                  
                  mysql> SELECT @rank:=@rank+1 AS rank, itemID, COUNT(*) as ordercount
                      -> FROM orders
                      -> GROUP BY itemID ORDER BY rank DESC;
                  +------+--------+------------+
                  | rank | itemID | ordercount |
                  +------+--------+------------+
                  |    5 |   3459 |          1 |
                  |    4 |    234 |          2 |
                  |    3 |    693 |          1 |
                  |    2 |   3432 |          1 |
                  |    1 |    388 |          3 |
                  +------+--------+------------+
                  5 rows in set (0.00 sec)
                  

                  推荐答案

                  看看这个.

                  将您的查询更改为:

                  SET @rank=0;
                  SELECT @rank:=@rank+1 AS rank, itemID, COUNT(*) as ordercount
                    FROM orders
                    GROUP BY itemID
                    ORDER BY ordercount DESC;
                  SELECT @rank;
                  

                  最后一个选择是你的计数.

                  The last select is your count.

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

                  上一篇:在 MySQL 中计算运行总数 下一篇:使用 MySQL,如何生成包含表中记录索引的列?

                  相关文章

                  最新文章

                    <bdo id='fotQr'></bdo><ul id='fotQr'></ul>

                • <tfoot id='fotQr'></tfoot>

                    <small id='fotQr'></small><noframes id='fotQr'>

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

                      <legend id='fotQr'><style id='fotQr'><dir id='fotQr'><q id='fotQr'></q></dir></style></legend>