Can you use two @rownum in the same select?

0

With this select I visualize the field of the table person with a sequential number before the field name .

SELECT @rownum:=@rownum+1 ‘fila’, nombre
FROM personal t, (SELECT @rownum:=0) r;

The output is something like this:

  

list / name
1 Luis
2 Carlos
3 Esteban

But I need to add another correlative and get something like this:

  

list / sublist / name
1 5 Luis
2 6 Carlos
3 7 Esteban

I tried to use something like this:

SELECT @rownum:=@rownum+1 ‘fila’, @rownum:=@rownum+1 ‘sublista’, nombre
FROM personal t, (SELECT @rownum:=0) r, (SELECT @rownum:=5) s

But I get this:

  

list / sublist / name
5 6 Luis
7 8 Carlos
9 10 Esteban

Is there any way to achieve what I need?

    
asked by Piropeator 12.01.2017 в 18:17
source

1 answer

0

The error is given because you are using the same varibale @numrow . You should create another, with a different name, for example @numrow2 .

Like this:

SELECT @rownum:=@rownum+1 ‘fila’, @rownum2:=@rownum2+1 ‘sublista’, nombre 
FROM personal t, (SELECT @rownum:=0) r, (SELECT @rownum2:=5) s

Or using variables with more intuitive names:

SELECT @fila:=@fila+1 ‘fila’, @sublista:=@sublista+1 ‘sublista’, nombre 
FROM personal t, (SELECT @fila:=0) r, (SELECT @sublista:=5) s
    
answered by 12.01.2017 / 18:22
source