Mysql - make each row increment 1

Asked

Viewed 2,774 times

4

I want to make an appointment sql and I want each row to have a column indicating the number of the ex row:

linha     nome
1          joao
2          maria
3          tiago
.          .
.          .
.          .
n          joares

But this "row" column I don’t have on the table, so I’m trying to do something like:

select count(nome), nome from pessoas;

The problem is that when I use Count() or sum() the result comes out grouped, as if I had done or group by name then how to proceed ?

  • Try this: http://stackoverflow.com/a/13550826/2256325

  • @Sergio thanks, does there exist another solution say so simpler where I perform only 1 instructions in the bank?

2 answers

4


To avoid the declaration you can use the variable as a table:

select 
    @num := @num + 1,
    u.usu_nome 
from tab_usuario u, (SELECT @num := 0) as t
group by u.usu_id;
  • That’s the solution I was looking for, thank you!

1

I found this solution:

set @num = 0 ;
select 
    @num := @num + 1 ,
    u.usu_nome from tab_usuario u
    group by u.usu_id;

But I wanted a solution only with select, without having to do this set of a variable

Browser other questions tagged

You are not signed in. Login or sign up in order to post.