I'm trying to insert into a table while avoiding a secondary query to lookup an id of a product which is contained in another table.
My insert query:
INSERT INTO prod_addfeatures (ProductID, ProdFeature, ProdFeatureTitle) VALUES (?, ?, ?);
ProductID is a Long Integer
, the other two are simple strings.
At this point in the program, I only have the string representation of the ProductID (the sku, ex: TB100).
To get the numerical id of the sku, I would do something like:
SELECT products.catalogid FROM products WHERE id = ?;
(id
is not a PK, rather catalogid
is... this is a vendor table and cannot be changed).
I'm trying to avoid doing the SELECT before running the INSERT in order to avoid the extra network round-trip (remote database), however I can really only seem to dig up examples of selecting into the table, not quite what I'm after.
For clarity:
Two of the Three values being inserted must be supplied at runtime and do not exist in any other table. One of the Three values being inserted must be looked-up at runtime as it resides in another table.
ProductID <-- Must be selected or joined from the products table
ProdFeature <-- Supplied at runtime
ProdFeatureTitle <-- Supplied at runtime
So I"m trying to get something like:
INSERT INTO prod_addfeatures (ProductID, ProdFeature, ProdFeatureTitle)
VALUES ((SELECT products.catalogid FROM products WHERE products.id = ?), ?, ?);
But I don't believe that is the correct way to go about this.