Multiple Inserts with a Subquery
Posted on March 20th, 2008 by Greg Allard in MySQL |
Inserting multiple rows into a table is simple.
INSERT INTO table1 (id) VALUES (1), (2)
But what if you want to use a subquery to replace the explicit query with something more dynamic. This should be possible, however, searching the internet didn’t get me (or my colleagues) any closer to figuring this out. The obvious attempt of replacing (1), (2) with (subquery) resulted in the error your subquery returns multiple records
In the end we discovered that removing VALUES from the query gave the desired effect.
INSERT INTO table1 (id) (SELECT id FROM table2)
Executing the above query will insert a row into table1 for every result returned by the subquery.
Add New Comment
Viewing 3 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
but damm, i can only think of the ga-zillions of scripts i need to rewrite
Do you already have an account? Log in and claim this comment.
http://dev.mysql.com/doc/refman/5.0/en/insert-s...
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks