sql - Adding an autoincrement column to query results -
without creating temp table autoincrement column, how can add auto-incrementing field query result? example,
select ???, * mytable should result in:
1|* 2|* 3|*
i think looking row numbers. try this:
select (select count(0) supportcontacts t1 t1.id <= t2.id ) 'row number', * supportcontacts t2 order id; example table:
╔════╦═════════╦═════════════════════╗ ║ id ║ type ║ details ║ ╠════╬═════════╬═════════════════════╣ ║ 1 ║ email ║ admin@sqlfiddle.com ║ ║ 2 ║ twitter ║ @sqlfiddle ║ ╚════╩═════════╩═════════════════════╝ result:
╔════════════╦════╦═════════╦═════════════════════╗ ║ row number ║ id ║ type ║ details ║ ╠════════════╬════╬═════════╬═════════════════════╣ ║ 1 ║ 1 ║ email ║ admin@sqlfiddle.com ║ ║ 2 ║ 2 ║ twitter ║ @sqlfiddle ║ ╚════════════╩════╩═════════╩═════════════════════╝
Comments
Post a Comment