Coocoo4Cocoa
Coocoo4Cocoa

Reputation: 50826

MySQL query (mixing an insert with select)

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.

Upvotes: 0

Views: 1420

Answers (2)

jonstjohn
jonstjohn

Reputation: 60276

insert into table1 (col1, col2, col3) select col1, col2, 2 
  from table2 where col3 = 1

Upvotes: 2

cletus
cletus

Reputation: 625077

Easy done.

INSERT INTO mytable
(a, b, c)
SELECT 2, b, c
FROM mytable
WHERE a = 1

Upvotes: 9

Related Questions