我有一些表设置并在如下的系统中链接:
产品
项
ItemsDataOne
ItemsDataTwo
我在以下选项中遇到问题,其中ProductIDs包含多个自己的实例:
select names.ProductID, names.Name, descriptions.Desc
from
(select Products.ProductID, ItemsDataOne.Value as Name
from Products
inner join Items on Items.ItemI = Product.ItemID
inner join ItemsDataOne ON ItemsDataOne.DataID = Items.DataID) as names
inner join
(select Products.ProductID, ItemsDataTwo.Value as Desc
from Products
inner join Items on Items.ItemI = Product.ItemID
inner join ItemsDataTwo ON ItemsDataTwo.DataID = Items.DataID) as descriptions
on names.ProductID = descriptions.ProductID
返回重复的内容如下:
实际结果
ProductID Name Description
01 "One" "Description One"
01 "Two" "Description One"
01 "One" "Description Two"
01 "Two" "Description Two"
我想要什么
ProductID Name Description
01 "One" "Description One"
01 "Two" "Description Two"
答案 0 :(得分:3)
如果没有子选择,可以更简单地完成:
select Products.ProductID, ItemsDataOne.Value, ItemsDataTwo.Value
from Products
inner join Items on Items.ItemID = Product.ItemID
inner join ItemsDataOne ON ItemsDataOne.DataID = Items.DataID
inner join ItemsDataTwo ON ItemsDataTwo.DataID = Items.DataID
(您错过了ItemsDataOne和ItemsDataTwo之间的关系 - 他们需要具有相同的DataID。)