tags:

views:

125

answers:

2

Hi,

I need to insert data from table1 into table2. However, I would like to set the myYear column in table2 to 2010. But, there isn't a myYear Column in table1.

So, my basic insert looks like:

INSERT INTO  `table2` ( place, event ) 
SELECT place, event
FROM table1

Roughly, I'd like to do something like the following:

INSERT INTO `table2` ( place, event, SET myYear='2010' )
...

Is there a way to set the column value in the insert statement?

THANK YOU!

-Laxmidi

+4  A: 

The following should do it:

INSERT INTO `table2` (place, event, myYear) 
SELECT place, event, '2010'
FROM   table1;

Basic test case:

CREATE TABLE t1 (a int, b int);
CREATE TABLE t2 (c int);

INSERT INTO t2 VALUES (1),(2),(3),(4),(5);

INSERT INTO t1 SELECT c, 100 FROM t2;

SELECT * FROM t1;

+------+------+
| a    | b    |
+------+------+
|    1 |  100 | 
|    2 |  100 | 
|    3 |  100 | 
|    4 |  100 | 
|    5 |  100 | 
+------+------+
5 rows in set (0.00 sec)
Daniel Vassallo
Hi Daniel, It worked perfectly. Thanks so much.-Laxmidi
Laxmidi
+2  A: 
INSERT INTO `table2` (place, event, myYear)
SELECT place, event, 2010
FROM table1

Edit: bah, didn't get the answer posted notification :P

Dusty
Hi Dusty, Thanks for the correct answer. Daniel beat you by 3 minutes ;)Thanks for the help.-Laxmidi
Laxmidi
Yeah, hence my edit :P
Dusty