views:

424

answers:

2

I have a bunch of rows in a table with columns a, b, c. I'd like to be able to SELECT all rows where say a = 1, and reinsert them with a = 2. Essentially keeping all the rows where column a exist as is, and having a new batch of rows having as a = 2. What's the best query to establish such a multi-INSERT query? This is all happening in the same table. I don't mind using a temporary one if it's required.

+9  A: 

Easy done.

INSERT INTO mytable
(a, b, c)
SELECT 2, b, c
FROM mytable
WHERE a = 1
cletus
is there any danger of creating infinite loops using this syntax?
nickf
There's no crossover between what you're inserting (a = 2) and what you're selecting (a = 1) but even if there were I don't believe it works that way, meaning your inserts aren't picked up by your select.
cletus
+2  A: 
insert into table1 (col1, col2, col3) select col1, col2, 2 
  from table2 where col3 = 1
jonstjohn