【问题标题】:Joining product attributes table with the product table to display product将产品属性表与产品表连接以显示产品
【发布时间】:2015-08-29 07:28:41
【问题描述】:

我有三个表用于列出具有产品属性的产品

产品表含虚拟数据

Product_Attributes 带有虚拟数据

属性带有虚拟数据

Kespersky antivirus (productid = 1) 没有属性,但 iPhone (productid =2) 有两个属性适用于它,内存和分辨率都在 Attribute 表中,其值存储在 Product_Attribute 表。

如何加入这些表格以显示/显示具有相应属性的两个产品?

编辑

我需要将这些产品显示为

【问题讨论】:

  • 您能否提供此虚拟数据所需的结果,以便我们查看您尝试实现的格式?
  • @Mureinik,当然。给我几分钟,我会修改我的问题。
  • @Mureinik,我已经编辑了我的问题。
  • 使用适当的报告工具,而不是强制 SQL 以您想要的方式显示数据!
  • @DakshB 你能看看我下面的答案吗?它适用于任意数量的属性,并以您上面给出的格式返回结果。

标签: mysql sql database join left-join


【解决方案1】:

你可以试试这个:

SELECT 
  P.ProductName,
  P.Price,
  -- Add other Column Here
  A.AttributeName,
  PA.AttributeValue
FROM Product P
LEFT JOIN Product_Attributes PA
  ON P.ProductID = PA.ProductID
LEFT JOIN Attributes A
  ON PA.AttributeID = A.AttributeID

输出

ProductName   Price    AttbituteName    AttributeValue
Kaspersky     380      NULL             NULL   
IPHONE        45000    Memory           64 gb
IPHONE        45000    Resolution       21500 pi

【讨论】:

  • 我已经编辑了我的问题。你能再看看吗?
  • 是内存和分辨率列的名称吗?
  • 你是说内存和分辨率吗?
  • 内存和分辨率都是我在创建表格后插入的值/记录。
  • 是的,我知道那些是属性表的记录。顺便说一句,您希望以哪种方式获得结果,Test#1 或 Test#2。 heypasteit.com/clip/20T2
【解决方案2】:

您的问题需要一个支点,需要预先定义。这意味着,如果您想在结果集中包含 2 个额外的 COLUMNS,那么您的查询最多只能存储 2 个属性。这是一个 PRESENTATION 层问题,而不是查询层。但是,唉,我有一个通用的解决方案给你。它假设您将拥有最多 2 个属性(出于上述原因)。这是查询:

SELECT 
  P.ProductName,
  A.AttributeName,
  PA.AttributeValue,
  B.AttributeName,
  PB.AttributeValue
FROM lb_products P
LEFT JOIN (select row_number() over (partition by productID order by AttributeID asc) rn, * 
           from lb_product_attributes x) PA
  ON P.ProductID = PA.ProductID and PA.rn = 1
LEFT JOIN (select row_number() over (partition by productID order by AttributeID asc) rn, * 
           from lb_product_attributes x) PB
  ON P.ProductID = PB.ProductID and PB.rn = 2
LEFT JOIN lb_attributes A
  ON PA.AttributeID = A.AttributeID
LEFT JOIN lb_attributes B
  ON PB.AttributeID = B.AttributeID;

还有供您玩耍的 SQL Fiddle。祝你好运!并随时提出任何问题:)

http://sqlfiddle.com/#!6/49a9e0/5

【讨论】:

    【解决方案3】:

    Philip's 的回答当然很好,他说明了问题在于,如果你正在做一个数据透视,你需要定义一个静态数量的属性。不过,我不确定窗口函数是否必要,所以我会这样做:

    select
      prd.productId
      ,max(case when lpa.attributeId = 1 then attributeName else null end) attributeName1
      ,max(case when lpa.attributeId = 1 then attributeValue else null end) attributeValue1
      ,max(case when lpa.attributeId = 2 then attributeName else null end) attributeName2
      ,max(case when lpa.attributeId = 2 then attributeValue else null end) attributeValu2
    from
      lb_products prd
      left outer join lb_product_attributes lpa on lpa.productId = prd.productId
      left outer join lb_attributes atr on atr.attributeId = lpa.attributeId
    group by
      prd.productId
    

    【讨论】:

    • 窗口函数是必要的,因为我们不能保证属性 ID 是什么。至少定义中没有。我们只需要“前两个”。
    • 真的吗? lb_attributes 表的存在似乎证明了这一点。问题底部的显示引用了特定属性,这些属性在维度中具有特定的 attributeId 值。我没有看到问题中提到“前两个”。
    【解决方案4】:

    以下内容适用于任意数量的属性:

    select product.productId, product.name,
    group_concat(concat(attr.attributeName, ":", pa.attributeValue))
    from product
    left outer join product_attributes pa 
    on (pa.productId = product.productId)
    left outer join attributes attr 
    on (attr.attributeId = pa.attributeId)
    group by product.productId, product.name
    

    【讨论】:

    • 这不会创建单独的列。
    • @PhilipDevine 在我看来,这完全符合要求的结果:“如何加入这些表格以显示/显示具有相应属性的产品?”它适用于任意数量的属性,其他答案都无法实现
    • @danb,我会调查的。我只是有点困了2-3天。
    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 2020-01-25
    相关资源
    最近更新 更多