homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

Is there anything like a "foreach" in MySQL?

 6:03 am on Jan 11, 2010 (gmt 0)

Here's what I want to do.

for each item n in table A,
I want to INSERT a row into table B.

I can get all the ID's I want like this:
SELECT * FROM tableA WHERE id > 500
which might return maybe a few thousand rows.

then for each of those, I want to do this:
INSERT INTO tableB (id,foo,bar) VALUES ( [id_from_previous_select] ,'abc','def')

I can write a PHP script to do it, with a loop. I know.
I'm curious if there's a faster way, a better way, using SQL commands only.



 7:45 pm on Jan 11, 2010 (gmt 0)

Why not just use the INSERT INTO .. SELECT syntax?

[INTO] tbl_name [(col_name,...)]
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]


 8:55 pm on Jan 11, 2010 (gmt 0)

oh yeah

I'll give that a try. thanks Dijkgraaf

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved