在mysql中选择增量计数器 [英] select increment counter in mysql

查看:120
本文介绍了在mysql中选择增量计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的mysql查询

  

我想选择一个增量计数器边的名称。如何做到这一点。
因此预期产量将

 周杰伦1 
罗伊2
拉维3
RAM 4


解决方案

 选择名称,
@rownum:由名$ b $ R
顺序:= @rownum + 1从your_table
交叉ROW_NUMBER
加入(= 0选择@rownum) b

此部分:

  cross join(select @rownum:= 0)r 

而不需要单独的查询。所以第一个查询也可以用2个不同的查询,如下:

  set @rownum:= 0; 

选择名称,
@rownum:= @rownum + 1从your_table
为了ROW_NUMBER
的名字

例如在存储过程中使用。


Here is my mysql query

select name from table;

I want to select an increment counter along side name.How to do this. so the expected output will be

Jay 1
roy 2
ravi 3
ram 4

解决方案

select name,
      @rownum := @rownum + 1 as row_number
from your_table
cross join (select @rownum := 0) r
order by name

This part:

cross join (select @rownum := 0) r

makes it possible to init a variable without the need of a seperate query. So the first query can be also made with 2 different queries like this:

set @rownum := 0;

select name,
      @rownum := @rownum + 1 as row_number
from your_table
order by name

for instance when used in a stored procedure.

这篇关于在mysql中选择增量计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆