重构SQL查询以将结果返回到行而不是列中
我有一个SQL查询需要重构。基本上,查询获取指定客户订购的所有ProductType。问题是结果以列而不是行的形式返回。这需要以另一种方式进行更改,以使查询更通用 那么这就是查询返回的结果:重构SQL查询以将结果返回到行而不是列中,sql,refactoring,Sql,Refactoring,我有一个SQL查询需要重构。基本上,查询获取指定客户订购的所有ProductType。问题是结果以列而不是行的形式返回。这需要以另一种方式进行更改,以使查询更通用 那么这就是查询返回的结果: Name ProductType1 ProductType2 ProductType3 -------------------------------------------------- Marc PT09 P15 PT33 Name Produ
Name ProductType1 ProductType2 ProductType3
--------------------------------------------------
Marc PT09 P15 PT33
Name ProductType
----------------
Marc PT09
Marc P15
Marc PT33
SELECT
CustomerData.Name as Name
Product1.productType as ProductType1,
Product2.productType as ProductType2,
Product3.productType as ProductType3
FROM
(SELECT ProductID, Name
FROM
Customer
Orders
WHERE Customer.ID = 111
) as CustomerData
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'A'
AND CP.ProductCategoryID = PC.ID
) as Product1
on CustomerData.ProductID = Product1.ProductID
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'B'
AND CP.ProductCategoryID = PC.ID
) as Product2
on CustomerData.ProductID = Product1.ProductID
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'C'
AND CP.ProductCategoryID = PC.ID
) as Product3
on CustomerData.ProductID = Product1.ProductID
这就是它应该的样子:
Name ProductType1 ProductType2 ProductType3
--------------------------------------------------
Marc PT09 P15 PT33
Name ProductType
----------------
Marc PT09
Marc P15
Marc PT33
SELECT
CustomerData.Name as Name
Product1.productType as ProductType1,
Product2.productType as ProductType2,
Product3.productType as ProductType3
FROM
(SELECT ProductID, Name
FROM
Customer
Orders
WHERE Customer.ID = 111
) as CustomerData
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'A'
AND CP.ProductCategoryID = PC.ID
) as Product1
on CustomerData.ProductID = Product1.ProductID
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'B'
AND CP.ProductCategoryID = PC.ID
) as Product2
on CustomerData.ProductID = Product1.ProductID
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'C'
AND CP.ProductCategoryID = PC.ID
) as Product3
on CustomerData.ProductID = Product1.ProductID
这是我简化了一点的查询:
Name ProductType1 ProductType2 ProductType3
--------------------------------------------------
Marc PT09 P15 PT33
Name ProductType
----------------
Marc PT09
Marc P15
Marc PT33
SELECT
CustomerData.Name as Name
Product1.productType as ProductType1,
Product2.productType as ProductType2,
Product3.productType as ProductType3
FROM
(SELECT ProductID, Name
FROM
Customer
Orders
WHERE Customer.ID = 111
) as CustomerData
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'A'
AND CP.ProductCategoryID = PC.ID
) as Product1
on CustomerData.ProductID = Product1.ProductID
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'B'
AND CP.ProductCategoryID = PC.ID
) as Product2
on CustomerData.ProductID = Product1.ProductID
LEFT JOIN (SELECT DISTINCT CP.ProductID as ProductID,
PC.Type as ProductType
FROM
CustomerProduct CP,
ProductCategory PC
WHERE
PC.Category = 'C'
AND CP.ProductCategoryID = PC.ID
) as Product3
on CustomerData.ProductID = Product1.ProductID
因此,我一直在考虑将连接拆分为一个单独的存储过程,然后调用它,因为我需要更多的ProductType,但我似乎无法让它正常工作。有没有人能想出办法让它工作起来?在专栏中做事通常要困难得多 假设客户、产品和订单都是标准化的表,您不需要做任何事情,只需要:
SELECT C.customer_name
, P.product_type
FROM Customers C
JOIN Orders O
ON O.customer_id=C.customer_id
JOIN Products P
ON O.product_id=P.product_id
WHERE C.ID = 111
如果这不起作用,请列出相关表的结构。我假设您的表看起来像这样 顾客
id | name
11 | Marc
产品
id | type
21 | PT09
22 | P15
23 | PT33
命令
id | id_customer | id_product | quantity
31 | 11 | 21 | 4
32 | 11 | 22 | 6
33 | 11 | 23 | 8
那么你的问题是
SELECT
a.name,
c.type
FROM
Customer a
LEFT JOIN
Orders b ON b.id_customer = a.id
LEFT JOIN
Products c ON c.id = b.id_product
在执行查询之前,是否知道产品类型的数量?如果是这样的话,您可以使用多个联合动态创建SQL。+1是的,就是这样做的。它可能只需要
LEFT JOIN
而不是JOIN
,这取决于他的数据和他想要显示的内容。或者,如果客户有许多相同产品的订单,而您只想看到其中任何一个(订单)的一行,则可以使用分组方式。如果他想显示或过滤ProductCategory,也可以再加入一次。但这是一条路要走。此外,查询看起来简单而优雅:不到10行代码,而不是40多行复杂的4个子查询。