请提供7个相关表格中复杂连接的帮助

时间:2014-01-25 12:39:53

标签: sql sql-server tsql sql-server-2012

我有一张包含ProductID的产品表(Name)。我有一个名为ProductCondition的链接表,其中包含3个字段(ID, ProductID, ConditionID)。每个产品可以有多个Condition

我需要一个查询,它会为所有产品提供与Condition相同的Product ID

要使问题复杂化,必须使用IngredientEnd Use执行相同操作,但这些是或条件。

我需要所有具有相同条件或最终用途或成分的产品作为单一提供的ProductID

CREATE TABLE [dbo].[Product](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NULL,
[Image] [nvarchar](max) NULL,
[Description] [nvarchar](max) NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


CREATE TABLE [dbo].[Condition](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](255) NULL,
[Description] [varchar](max) NULL,
CONSTRAINT [PK_Condition] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


CREATE TABLE [dbo].[ProductCondition](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ProductID] [int] NOT NULL,
[ConditionID] [int] NOT NULL,
[ProductNotRecommended] [bit] NULL,
CONSTRAINT [PK_ProductCondition_1] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Product
ID Name
1  Product 1
2  Product 2
3  Product 3
4  Product 4
5  Product 5

Condition
ID Name
1  Condition 1
2  Condition 2
3  Condition 3
4  Condition 4
5  Condition 5

ProductCondition
ID  ProductID  ConditionID
1   1          1
2   1          2
3   2          1
4   3          3
5   4          2


Given a ProductID of 1, it has the following conditions:
1
2

So now I want to get all products that have a condition ID of 1 or 2
Product
ID  Name
1  Product 1
2  Product 2
3  Product 3
5  Product 5

这只是'条件'的情况。 “最终用途”和“成分”要求完全相同。

如果我给出a的ID,并且它有条件e和f,h,j和m的结束使用,以及p的成分,我想要所有条件为e或f的产品,或者结束使用h或j或m,或p。

的成分

以下是我的开始:

SELECT *
FROM Product p
--End Use
LEFT JOIN ProductEndUse pe ON p.ID = pe.ProductID
LEFT JOIN EndUse e ON e.ID = pe.EndUseID
-- Condition
LEFT JOIN ProductCondition pc ON p.ID = pc.ProductID
LEFT JOIN Condition c ON c.ID = pc.ConditionID
-- Ingredient
LEFT JOIN ProductIngredient pi ON p.ID = pi.ProductID
LEFT JOIN Ingredient i ON i.ID = pi.IngredientID

CROSS JOIN Product p2

-- [SOMETHING I don't know]

WHERE p2.ID = 1

1 个答案:

答案 0 :(得分:0)

获取每个类别匹配的产品列表,并将它们合并在一起。

DECLARE @productid int;
set @productid = 1; --this is product to search for related items

SELECT p.ID as RelatedProductID, p.name as RelatedProductName, 'EndUse' as MatchType, e.name as MatchValue
FROM Product p
--End Use
INNER JOIN ProductEndUse pe ON p.ID = pe.ProductID
INNER JOIN EndUse e ON e.ID = pe.EndUseID
WHERE e.ID IN (select e2.EndUseID from product p2 inner join ProductEndUse e2 on p2.productid = e2.productid where p2.productid = @productid)

UNION

SELECT p.ID as productID, p.name as ProductName, 'Condition' as MatchType, c.name as MatchValue
FROM Product p
-- Condition
INNER JOIN ProductCondition pc ON p.ID = pc.ProductID
INNER JOIN Condition c ON c.ID = pc.ConditionID
WHERE c.ID IN (select pc2.ConditionID from product p2 inner join Condition pc2 on p2.productid = pc2.productid where p2.productid = @productid)

UNION

SELECT p.ID as productID, p.name as ProductName, 'Ingredient' as MatchType, i.name as MatchValue
FROM Product p
-- Ingredient
INNER JOIN ProductIngredient pi ON p.ID = pi.ProductID
INNER JOIN Ingredient i ON i.ID = pi.IngredientID
WHERE i.ID IN (select pi2.IngredientID from product p2 inner join Ingredient pi2 on p2.productid = pi2.productid where p2.productid = @productid)    

如果您只想要产品的不同列表,而不显示匹配的内容,请从每个部分中删除MatchTypeMatchValue列。