2016-11-08 92 views
1

我有以下查詢....SQL UNION查詢到另一個表來選擇數據

SELECT t1.ProductID, t2.Name as [Product Type], t3.Name as [Product Category], 
t4.Name as [Product Provider] 
From tblProducts t1 
Inner Join tblReferences t2 on t1.ProductType = t2.ID 
Inner Join tblReferences t3 on t1.ProductCategory = t3.ID 
Inner Join tblReferences t4 on t1.ProductProvider = t4.ID 

我知道這是不是最漂亮的查詢,但它的工作,而且很可能不會編輯完成後進行編輯。現在我試圖做一個內部聯接與另一個表,這裏是問題來...

所以我想從另一個表(tblProductSeller),但我需要它來顯示作爲[產品供應]

所以在前面的SELECT語句添加...

 SELECT t1.ProductID, t2.Name as [Product Type], t3.Name as [Product Category], 
t4.Name as [Product Provider], 

t6.SellerName as [Product Provider] <---new line - 

然而,這顯示它作爲一個新的專欄,我想在同一列中顯示它作爲

t4.Name as [Product Provider] 

在我試圖查詢的其餘部分是

Inner Join tblReferences t2 on t1.ProductType = t2.ID 
Inner Join tblReferences t3 on t1.ProductCategory = t3.ID 
Inner Join tblReferences t4 on t1.ProductProvider = t4.ID 
Inner Join tblProductSeller t6 on t1.ProductProvider = t6.ID 

是一樣的東西,即使是可能的 - 我要做一個UNION?

編輯。爲了保持簡短,我會放棄大部分字段,但我遇到問題的例外情況除外。

TblProducts 
ProductID   ProviderID   
17     16 
18     20 
19     24 

tblReferences 
ID     Name 
16     Microsoft 
20     ADP 

tblProductSeller 
ID     ProductProvider 
24     Apple 

TblReferences有一些標準的賣家的名字,但是,我需要能夠引用tblProductSeller,因爲供應商的很多將在那裏加入。基本上這是他們將被添加的地方。我所有的數據都存儲在tblProducts中。

我的整個目標是能夠填充DataGridView,但是,沒有任何ID,而是能夠引用tblReferences和tblProductSeller。

所以我最終的結果會是什麼樣子

ProductID  Contact Provider 
16    Microsoft 
20    ADP 
24    Apple 
+0

如果我可以提供更多的信息,請讓我知道。 – BobSki

+1

顯示在同一列意味着什麼?拼接它?你可以做'(t4.Name +''+ t6。SellerName)作爲[產品供應商]' – Mihai

+0

@mihil不連接。在tblProducts中,我爲產品供應商保存了整數(ID)。我試圖從tblReferences和tblProductSeller中選擇Contact Provider TEXT。由於某些產品供應商位於tblProductSeller中,因此並非所有ID都可以在tblReferences中引用,所以我需要它爲實際的TEXT值而不是ID引用這兩個值。 – BobSki

回答

2

根據不同的使用情況下,以所需要的表的語句需要進行以下調整,但只是爲了讓您的車輪轉動。 ..這裏是一個例子。

SELECT t1.ProductID, 
      t2.Name as [Product Type], 
      t3.Name as [Product Category], 
      t4.Name as [Product Provider] 
    From tblProducts t1 
    Inner Join tblReferences t2 
    on t1.ProductType = t2.ID 
    Inner Join tblReferences t3 
    on t1.ProductCategory = t3.ID 
    Inner Join tblReferences t4 
    on t1.ProductProvider = t4.ID 
UNION 
    Select t1.ProductID, 
      t2.Name as [Product Type], 
      t3.Name as [Product Category], 
      t6.Name as [Product Provider] 
    From tblProducts t1 
    Inner Join tblReferences t2 
    on t1.ProductType = t2.ID 
    Inner Join tblReferences t3 
    on t1.ProductCategory = t3.ID 
    Inner Join tblProductSeller t6 
    on t1.ProductProvider = t6.ID 
+0

做這項工作。謝謝你,先生! – BobSki

+1

沒問題,很高興我能幫到你。 –

+1

看起來像一個'LEFT JOIN'和'COALESCE()'會容易得多......當然,如果當't4.Name'不同於't6.SellerName'時需要額外的一行,那麼'UNION'就是可能是要走的路。 – ebyrob

1

這裏是一個替代的解決方案,如ebyrob描述:

SELECT t1.ProductID, 
     t2.Name as [Product Type], 
     t3.Name as [Product Category], 
     IsNull(t4.Name, t6.Name) as [Product Provider] 
From tblProducts t1 
Inner Join tblReferences t2 on t1.ProductType = t2.ID 
Inner Join tblReferences t3 on t1.ProductCategory = t3.ID 
LEFT Join tblReferences t4 on t1.ProductProvider = t4.ID 
LEFT Join tblProductSeller t6 on t1.ProductProvider = t6.ID; 

這將完成的是顯示從tblReferencesName如果有的話,否則它會顯示從tblProductSellerName。您可以根據您的要求重新排序。您也不妨加一個謂詞像

WHERE t4.ID IS NOT NULL OR t6.ID IS NOT NULL

這將避免在有兩個tblReferencestblProductSeller匹配的情況返回多個行。

+0

這就是我遇到的問題。某些ID都在tblReferences和tblProductSeller中。現在對於其中一些人,我需要從tblReferences和tblProductSeller中提取一些。 – BobSki

+0

你怎麼知道你需要從每張桌子上得到哪一張? @Bobski – mendosi

+0

我有點解決了問題,將tblProductSeller中的ID更改爲一個不同的數字,所以當我將它保存並在我的查詢中引用tblReference時,它沒有找到它 – BobSki

相關問題