重构 SQL 查询以将结果返回到行而不是列中

发布于 2024-10-24 03:32:11 字数 2015 浏览 0 评论 0原文

我有一个需要重构的 SQL 查询。基本上,查询获取指定客户订购的所有产品类型。问题在于结果以列而不是行的形式返回。这需要以相反的方式进行更改以使查询更加通用。

这就是查询返回的内容:

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

所以我一直在考虑将连接分割成一个单独的存储过程,然后调用它,因为我需要更多的产品类型,但我似乎无法让它工作。有人知道如何让它发挥作用吗?

I have a SQL query that need to be refactored. Basically the query gets all the producttypes ordered by a specified customer. The problem is that the results are returned in columns instead of rows. This needs to be changed the other way around to make the query more generic.

So this is what the query returns:

Name   ProductType1   ProductType2   ProductType3
--------------------------------------------------
Marc   PT09           P15            PT33

And this is what it should be:

Name ProductType
----------------
Marc PT09
Marc P15
Marc PT33

This is the query which I have simplified a bit:

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

So I have been thinking about splitting the joins into a separate stored proc and then call this as I need more productTypes but I can't seem to get this working. Anyone an idea on how to get this working ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽梦忆笙歌 2024-10-31 03:32:12

在列中做事实际上通常要困难得多。

假设规范化表“客户”、“产品”和“订单”,您不需要做任何其他事情,而只是:

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

如果这不起作用,请列出相关表的结构。

Doing things in columns is actually usually much more difficult.

Assuming normalized tables Customers, Products and Orders, you shouldn't need to do anything more than just:

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

If this doesn't work, please list structures of the involved tables.

淡淡の花香 2024-10-31 03:32:12

我假设您的表看起来像这样

Customer

id | name
11 | Marc

Products

id | type
21 | PT09
22 | P15
23 | PT33

Orders

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 

I'm going to assume your tables looks something like this

Customer

id | name
11 | Marc

Products

id | type
21 | PT09
22 | P15
23 | PT33

Orders

id | id_customer | id_product | quantity
31 | 11          | 21         | 4
32 | 11          | 22         | 6
33 | 11          | 23         | 8

Then your query is

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文