我有以下查詢....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
如果我可以提供更多的信息,請讓我知道。 – BobSki
顯示在同一列意味着什麼?拼接它?你可以做'(t4.Name +''+ t6。SellerName)作爲[產品供應商]' – Mihai
@mihil不連接。在tblProducts中,我爲產品供應商保存了整數(ID)。我試圖從tblReferences和tblProductSeller中選擇Contact Provider TEXT。由於某些產品供應商位於tblProductSeller中,因此並非所有ID都可以在tblReferences中引用,所以我需要它爲實際的TEXT值而不是ID引用這兩個值。 – BobSki