Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Create table from select then alter table by adding an auto increment column in mysql

0

158 views

Hi all,

as briefly explained in subject, I need to create a table by selecting existing value. The thing I would like to achieve is to have another column with auto incremented value.

This is what I already tried:

CREATE TEMPORARY TABLE temp_tb (    
    `row_id` bigint(20) NOT NULL AUTO_INCREMENT,
    `stm_id` bigint(20) NOT NULL,
    descr varchar(20) NOT NULL,
    PRIMARY KEY (row_id)
);

Then after with a select:

INSERT INTO temp_tb (
  select stm_id,descr from tb_export
)

I was expecting to have the row_id column prefilled at insert time, but I just got sql syntax error telling me that column count doesn't match value count.

Do you know if this is possible to achieve ?

thanks

asked January 11, 2011 11:13 am CST
posted via StackOverflow

2 Answers

0
 

If I am not mistaken, the syntax error has to do with your insert syntax. You have a temp_tb that has 3 fields, all not null. You are inserting 2 fields into that table with your insert statement. The MySQL ref lists the syntax for insert using select as:

INSERT INTO tbl_temp2 (fld_id) SELECT tbl_temp1.fld_order_id FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

answered January 11, 2011 11:39 am CST
PDI
0
 

you should provide the names of the columns you are inserting into your temp_tb:

INSERT INTO temp_tb (stm_id, descr) (
  select stm_id,descr from tb_export
)

answered January 11, 2011 11:39 am CST

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions