Welcome to WebmasterWorld Guest from 54.160.163.163

Forum Moderators: open

Message Too Old, No Replies

Is there anything like a "foreach" in MySQL?

     

httpwebwitch

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

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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

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

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



oh yeah

I'll give that a try. thanks Dijkgraaf

 

Featured Threads

Hot Threads This Week

Hot Threads This Month