tags:

views:

70

answers:

2

I have a large number of rows that I would like to copy, but I need to change one field.

I can select the rows that I want to copy:

select * from Table where Event_ID = "120"

Now I want to copy all those rows and create new rows while setting the Event_ID to 155. How can I accomplish this?

+6  A: 
INSERT INTO Table
          ( Event_ID
          , col2
           ...
          )
     SELECT "155"
          , col2
           ...
      FROM Table WHERE Event_ID = "120"

Here, the col2, ... represent the remaining columns (the ones other than Event_ID) in your table.

dcp
is there any way to do it without having to specify the column names?
Andrew
Not that I'm aware of. Of course, if your table has like 1000 columns or something and you don't want to type them all, then you could write a SQL statement to build your SQL statement :). The way you would do it would be to use the information_schema to get the column names for the table. But that's really overkill, I'd just type out the column names.
dcp
+2  A: 

Let's say your table has two other columns: foo and bar

INSERT INTO Table (foo, bar, Event_ID)
SELECT foo, bar, "155"
  FROM Table
 WHERE Event_ID = "120"
Peter Bailey
is there any way to do it without having to specify the column names?
Andrew