如何在 MySQL 查询结果中显示序列号

2021-12-19 00:00:00 select mysql

我有一些简单的查询:

SELECT foo, bar FROM table

我想你现在的结果是什么样的.

i think you now whats the result looks like.

我想要做的是根据查询结果中出现的数据数量来显示一些序列号.它就像 AUTO_INCREMENT(这并不意味着我想显示 ID).我想要的结果是这样的:

What I want to do is to show some sequential number based on how many data appear from query result. its just like AUTO_INCREMENT(its not mean i want to show ID). The result what I want is like:

|No|   foo   |    bar   |
-------------------------
|1 |   bla   |    123   |
|2 |   boo   |    abc   |
|3 |   wow   |    xxx   |

我该怎么做才能做到?

提前致谢

推荐答案

select @rownum:=@rownum+1 No, foo, bar from table, (SELECT @rownum:=0) r;

相关文章