开发者

show row number in select query

开发者 https://www.devze.com 2023-02-02 13:36 出处:网络
what is the problem with this query? it shows null in rowno column. SELECT @rowno:=@rowno+1 `rn`,`id`, `tit开发者_如何转开发le`, `topic`

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

SELECT @rowno:=@rowno+1 `rn`,`id`, `tit开发者_如何转开发le`, `topic` 
FROM stories,(SELECT @rownum:=0) r 
WHERE newstype='2';

i run it in 'MySQL Query browser'

thanks in advance.


You have a few problems there:

  • You need to initialise @rowno by adding set @rowno = 0 before the query.
  • You're missing an as in @rowno:=@rowno+1 rn.
  • The (SELECT @rownum:=0) r is superfluous, unless you meant this to be the initialisation for @rowno in which case you misspelt it.

This should work:

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


In the increment you are using rowno but in the initial assignment you are using rownum

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号