1

I would like to select from a table and include in this select a column that is not existing in the table and it is a integer auto - incrementing.

Like:

SELECT username, 'c' as C FROM users;

where c doesn't exist in the table, but should be integer and auto-increment.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
Stefano Maglione
  • 3,946
  • 11
  • 49
  • 96

1 Answers1

1
SELECT username, @rank := @rank + 1 as rank
FROM users
CROSS JOIN (select @rank := 0) r
ORDER BY username

So

CROSS JOIN (select @rank := 0) r

inits a variable named rank. And

@rank := @rank + 1 as rank

increments the variable for every row.

juergen d
  • 201,996
  • 37
  • 293
  • 362