MySQL 在选择查询中显示行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4573699/
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 18:10:06  来源:igfitidea点击:

show row number in select query

mysqlsql

提问by hd.

what is the problem with this query? it shows null in rowno column.

这个查询有什么问题?它在 rowno 列中显示为空。

SELECT @rowno:=@rowno+1 `rn`,`id`, `title`, `topic` 
FROM stories,(SELECT @rownum:=0) r 
WHERE newstype='2';

i run it in 'MySQL Query browser'

我在“MySQL 查询浏览器”中运行它

thanks in advance.

提前致谢。

回答by moinudin

You have a few problems there:

你有几个问题:

  • You need to initialise @rownoby adding set @rowno = 0before the query.
  • You're missing an asin @rowno:=@rowno+1 rn.
  • The (SELECT @rownum:=0) ris superfluous, unless you meant this to be the initialisation for @rownoin which case you misspelt it.
  • 您需要@rowno通过set @rowno = 0在查询之前添加来初始化。
  • 你缺少一个asin 。@rowno:=@rowno+1 rn
  • (SELECT @rownum:=0) r,除非你的意思是这是用于初始化是多余的,@rowno在这种情况下,你拼错了。

This should work:

这应该有效:

SET @rowno = 0;
SELECT @rowno:=@rowno+1 as `rn`, `id`, `title`, `topic` 
FROM stories 
WHERE newstype='2';

回答by a_horse_with_no_name

In the increment you are using rownobut in the initial assignment you are using rownum

在您使用的增量中,rowno但在您使用的初始分配中rownum