sql - Inserting Record by looking up in another table(Access 2007) -
I have the datagrid view queue, which has three string values. These values should be seen in the product table to find related product IDs. Then this relationship will be put on the table. I am looking for the best query to get it.
Here is my product table ------------ + -------------- + | Product_id | Product_name | + ------------ + -------------- + | 1 | Foo | | 2 | Bar | 3 | Falcon | | 4 | New product | + ------------ + -------------- + And I'm trying to put in the connection table I
+ ------------ + ---------------- + -------- - -------- + | Product_id | Relatedproducts_id | Relatedproduct_id2 + ------------ + ---------------- + ----------------- + | 1 | 2 | Empty | | 2 | 3 | 1 | | 3 | Empty | Empty | + ------------ + ---------------- + ----------------- + < / Code> Below is a table, this is a sample datagrid view queue.
+ ------------ + - ------------ + --------------- + | ProdName | RelProd_Name | RelProd_Name2 | + ------------ + -------------- + --------------- + | New product | Foo | Bar + ------------ + -------------- + --------------- + < / Pre> I am trying to find the ID from this line and put it in the relation table.
I tried an enigmatic query but I do not know the right way to do this. Like .something,
PROD_RELATIONS (product_id, RelatedProd_Id, RelatedProd_Id2) Insert value (product_id select products, select which Product_Name = 'Newproduct'), (product_id products where Product_Name = 'Foo') Can anyone guide me on this?
A question like this will work with your current table structure:
< code> Prod_Relations (product_id, RelatedProd_ID1, RelatedProd_ID2) choose to put in t1.Product_ID, t2.PRoduct_ID, t1 from t3.Product_ID products, T2, where the product T3 TlkProduct_Name = 'Newproduct' and t2.Product_Name = 'Foo to appreciate and t3.Product_Name = 'bar' However, I will again turn into a simpler layout your relationship table with multiple rows per product: < Pre> Product_ID | Related Proud_ID ------------ + ----------------- 4 | 1 4 | 2 This means that you do not need to add more columns statement you put in that situation if you have more than two terms in a product:
Prod_releshn (Utpad_aidi, Snbnditprod_aidi) in INSERT Select T1.Product_ID, product T1 of T2.PRoduct_ID, product T2 where T1.Product_Name = 'NewProduct' and < P> You can always ask for it to get back to your product relations table in 2 column format if necessary Be SELECT t1.Product_ID, MIN (t1.RelatedProd_ID) AS [RelatedProd_ID1], as MIN (t2.RelatedProd_ID) [RelatedProd_ID2] T1 Join left Prod_Relations from Prod_Relations T2 ON2.product_ID = T1.Product_ID and T2.RelatedProd_ID & gt; T1 Related Prod_ID Group by T.Product_ID
Comments
Post a Comment