【问题标题】:Is there a way to get the row number in Mysql like the rownum in oracle [duplicate]有没有办法像oracle中的rownum一样获取Mysql中的行号[重复]
【发布时间】:2012-01-20 13:16:27
【问题描述】:

有没有办法像oracle中的rownum一样获取Mysql中的行号,如果没有,那么有没有间接的方法呢? 请提出建议。

【问题讨论】:

标签: mysql sql oracle


【解决方案1】:

在 MySQL 最终支持 modern SQL 之前,获得类似的唯一方法是:

SELECT @rownum:=@rownum + 1 as row_number, 
       t.*
FROM ( 
   < your original query goes here >
) t,
(SELECT @rownum := 0) r

【讨论】:

    【解决方案2】:

    最佳做法是为每一行添加一个自动递增 ID。如果有一天您选择使用关系表,这也会对您有很大帮助。

    在 phpMyAdmin 中,当您创建(或编辑)表格时,您会看到 AUTO_INCREMENT 选项。您只能启用一列启用自动 ID。

    编辑:这就是你的数据库的样子:

    id | name  | email | phone
    1    Tekin   ...     ...
    2    John    ...     ...
    3    Jane    ...     ...
    

    【讨论】:

    • 这与 row_number() 值完全不同。如果要按特定列排序后获取行号怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2021-02-28
    • 2023-03-25
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多