2012-07-12 71 views
2

我有一個datagridview行,包含三個字符串值。應將這些值查找到產品表以找到相應的產品ID。然後這些將被插入到關係表中。我正在尋找最佳查詢來實現這一目標。通過查找另一個表(Access 2007)插入記錄

Here is my Products Table 
+------------+--------------+ 
| Product_ID | Product_Name | 
+------------+--------------+ 
|   1 | Foo   | 
|   2 | Bar   | 
|   3 | Baz   | 
|   4 | NewProduct | 
+------------+--------------+ 

和關係表我試圖插入

+------------+----------------+-----------------+ 
| Product_Id | RelatedProd_Id | RelatedProd_Id2 | 
+------------+----------------+-----------------+ 
|   1 | 2    | null   | 
|   2 | 3    | 1    | 
|   3 | null   | null   | 
+------------+----------------+-----------------+ 

下面的一個不是表,這是一個樣本的datagridview行..

+------------+--------------+---------------+ 
| ProdName | RelProd_Name | RelProd_Name2 | 
+------------+--------------+---------------+ 
| NewProduct | Foo   | Bar   | 
+------------+--------------+---------------+ 

我想從這一行查找id並將其插入到關係表中。

我嘗試了啞巴query..but我不知道」做it..something像的正確方法,

INSERT INTO PROD_RELATIONS (Product_id,RelatedProd_Id,RelatedProd_Id2) 
VALUES 
(SELECT Product_Id FROM Products WHERE Product_Name = 'NewProduct'), 
(SELECT Product_Id FROM Products WHERE Product_Name = 'Foo'), 
(SELECT Product_Id FROM Products WHERE Product_Name = 'Bar') 

有人能指導我這個?

回答

2

根據您當前的表結構這樣的查詢會工作:

INSERT INTO Prod_Relations (Product_ID, RelatedProd_ID1, RelatedProd_ID2) 
SELECT t1.Product_ID, t2.PRoduct_ID, t3.Product_ID 
FROM Products t1, Products t2, Products t3 
WHERE T1.Product_Name = 'NewProduct' 
AND  t2.Product_Name = 'Foo' 
AND  t3.Product_Name = 'bar' 

不過,我建議你改變你的關係表,以一種更簡單的佈局,每個產品多行:

Product_ID | RelatedProd_ID 
------------+----------------- 
    4  |  1 
    4  |  2 

這意味着如果產品關係超過2個,則不必添加更多列。在這種情況下,你的插入語句應該是:

INSERT INTO Prod_Relations (Product_ID, RelatedProd_ID) 
SELECT t1.Product_ID, t2.PRoduct_ID 
FROM Products t1, Products t2 
WHERE T1.Product_Name = 'NewProduct' 
AND  t2.Product_Name IN ('Foo', 'Bar') 

您可以隨時查詢您的產品的關係表,如果有必要

SELECT t1.Product_ID, 
     MIN(t1.RelatedProd_ID) AS [RelatedProd_ID1], 
     MIN(t2.RelatedProd_ID) AS [RelatedProd_ID2] 
FROM Prod_Relations t1 
     LEFT JOIN Prod_Relations t2 
      ON t2.Product_ID = t1.Product_ID 
      AND t2.RelatedProd_ID > t1.RelatedProd_ID 
GROUP BY t1.Product_ID 
+0

感謝它的工作!我認爲我對於更改表格佈局有了一些看法:-) ...我對SQL /表結構頗爲陌生...... – vinayan 2012-07-17 10:57:59

1

您可以使用Access' DLookup Function檢索拿回來給2列的格式您的價值INSERT INTO PROD_RELATIONS

INSERT INTO PROD_RELATIONS (
    Product_id, 
    RelatedProd_Id, 
    RelatedProd_Id2 
    ) 
VALUES (
    DLookup("Product_Id", "Products", "Product_Name = 'NewProduct'"), 
    DLookup("Product_Id", "Products", "Product_Name = 'Foo'"), 
    DLookup("Product_Id", "Products", "Product_Name = 'Bar'") 
    ); 

相比之下,與您的示例查詢,並注意個人SELECT件容易如何轉化爲DLookup()表達式。 DLookup非常類似於返回一個值的SELECT查詢。

如果按照Gareth的建議重新設計餐桌,則可以採用這種方法來適應該結構。

+0

與派生表相比,Dlookup也非常慢。 – Fionnuala 2012-07-12 09:17:11

+0

不,它並不總是一件壞事,但與派生表相比,速度較慢。我自己使用它很多,但沒有大量的數據。我在那種情況下爲任何正在考慮DLookup的人添加了評論。最近有幾個關於加速查詢的問題。 – Fionnuala 2012-07-12 09:29:34

相關問題