0

With MySQL I know it's possible to create a temporary table from a SELECT: Create a temporary table in a SELECT statement without a separate CREATE TABLE

I also know it's possible to create autoincrement in a temporary table: Auto Increment in Temporary Table

I want to do both at the same time: create a temporary table from a SELECT statement and I want the temporary table to have an auto increment column.

It is possible to do that?

Community
  • 1
  • 1
evg02gsa3
  • 571
  • 5
  • 17
  • Thank you but I must create the temporary table from the select itself (no create temporary table before) – evg02gsa3 Nov 06 '14 at 20:08

1 Answers1

3

If you want to create a temporary table with a row number column, then use variables:

create temporary table temp as
    select (@rn := @rn + 1) as seqnum, t.*
    from t cross join
         (select @rn := 0) vars;

This will not be auto-incrementing for new inserts. If you need that, you'll need to alter the table definition.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786