| Welcome to WebmasterWorld Guest from 184.108.40.206 |
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
|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?
INSERT [LOW_PRIORITY ¦ HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
| 8:55 pm on Jan 11, 2010 (gmt 0)|
I'll give that a try. thanks Dijkgraaf
All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
© Webmaster World 1996-2013 all rights reserved