homepage Welcome to WebmasterWorld Guest from 54.163.91.250
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?
httpwebwitch




msg:4058132
 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.

 

Dijkgraaf




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

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

INSERT [LOW_PRIORITY HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

httpwebwitch




msg:4058629
 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