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
  • 499,602
  • 82
  • 638
  • 795
Stefano Maglione
  • 3,695
  • 10
  • 43
  • 85

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
  • 195,137
  • 36
  • 275
  • 343