Welcome Guest, Not a member yet? Register   Sign In
MySQL: Inserting data based on insert_id, in a single query?
#1

[eluser]mddd[/eluser]
I am working on an import script. It will handle large amounts of data so I want to make it as efficient as I can. Part of the script is writing a product to the database, along with a record in a table that links products to categories.

So, basically:
Code:
INSERT INTO products (col1, col2) VALUES (val1, val2)
INSERT INTO products_categories (product, category) VALUES (product_id, category_id)

Of course, I only know the product_id for query #2 after I have inserted the product into the database through query #1.
So this will always take two calls to the database. Or is there a way to make both into one query? Something like (and I know this is not functioning code):
Code:
INSERT INTO products_categories (product, category) VALUES (
INSERT_ID_FROM_QUERY( INSERT INTO products (col1, col2) VALUES (val1, val2) ), category_id
)

Does something like this even exist?


Messages In This Thread
MySQL: Inserting data based on insert_id, in a single query? - by El Forum - 05-04-2010, 09:18 AM



Theme © iAndrew 2016 - Forum software by © MyBB