MySQL 有没有办法像oracle中的rownum一样获取Mysql中的行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8509996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:02:18  来源:igfitidea点击:

Is there a way to get the row number in Mysql like the rownum in oracle

mysqlsqloracle

提问by Sashi Kant

Is there a way to get the row number in Mysql like the rownum in oracle, If not then is there any indirect way of doing it? please suggest.

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

回答by a_horse_with_no_name

Until MySQL finally supports modern SQL, the only way to get something similar is this:

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

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

回答by Sashi Kant

The best practice is to add an auto incrementing IDfor each row. This would also help you a lot if, one day, you choose to use relational tables.

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

In phpMyAdmin, when you create (or edit) a table, you'll see the AUTO_INCREMENToption. You can only have one column with auto IDs enabled.

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

Edit: This is how you database would look like:

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

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