Mysql - make each line increment 1

4

I want to make a query sql and I want each line to have a column indicating the line number eg:

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

But, this column "line" I do not have in the table, so I'm trying to do something like:

select count(nome), nome from pessoas;
The problem is that when I use sum () or sum like I did or group by name then how to proceed?

    
asked by anonymous 22.05.2015 / 22:44

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;
    
22.05.2015 / 22:54
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 with only the select, without having to do this set of a variable

    
22.05.2015 / 22:47