Welcome to WebmasterWorld Guest from 54.226.146.15

Forum Moderators: open

Message Too Old, No Replies

using subquery within an INSERT statement (with multiple rows)

can i insert multiple rows in one statement?

     

techtheatre

9:51 am on Dec 22, 2009 (gmt 0)

5+ Year Member


So...it may simply not be possible, but I want to have an insert statement (or whatever) to insert new rows into my database with some info coming from another table. the problem is that this requires the insertion of multiple rows. I know I can do it one row at a time, but that is going to be VERY tedious (several thousand rows).

INSERT INTO PagePlugins
(PageName, PluginCode, PluginLabel)
VALUES
(
(SELECT DISTINCT PageName FROM PageData),'plug_weather','Local Weather'
)

I get the following error:
MySQl: #1242 - Subquery returns more than 1 row

whoisgregg

7:41 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Try this syntax instead:

INSERT INTO PagePlugins (PageName, PluginCode, PluginLabel)
SELECT DISTINCT PageName, 'plug_weather' AS PluginCode, 'Local Weather' AS PluginLabel
FROM PageData

techtheatre

8:16 pm on Dec 22, 2009 (gmt 0)

5+ Year Member



Awesome. I had way over-simplified the query in my post, but after tweaking my real query with this new format it works perfectly. Thanks!

whoisgregg

8:35 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Glad I could help. :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month