Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 将产品属性表与产品表联接以显示产品_Mysql_Sql_Database_Join_Left Join - Fatal编程技术网

Mysql 将产品属性表与产品表联接以显示产品

Mysql 将产品属性表与产品表联接以显示产品,mysql,sql,database,join,left-join,Mysql,Sql,Database,Join,Left Join,我有三个表列出了具有产品属性的产品 带有虚拟数据的产品表 带有虚拟数据的产品属性 属性带有虚拟数据 Kespersky antivirus(productid=1)没有属性,但iPhone(productid=2)有两个适用于它的属性,内存和分辨率都在属性表中,其值存储在产品属性表中 如何连接这些表以显示/显示具有相应属性的两个产品 编辑 我需要将这些产品显示为 您可以尝试以下方法: SELECT P.ProductName, P.Price, -- Add other

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

带有虚拟数据的产品表

带有虚拟数据的产品属性

属性带有虚拟数据

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

如何连接这些表以显示/显示具有相应属性的两个产品

编辑

我需要将这些产品显示为

您可以尝试以下方法:

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

你的问题需要一个支点,这个支点需要预先定义。也就是说,如果您想在结果集中包含两个额外的列,那么您的查询最多只能存储两个属性。这是一个表示层问题,而不是查询层问题。但是,唉,我有一个通用的解决方案给你。它假设您最多有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则是供您使用的小提琴。祝你好运并随时提出任何问题:)

答案当然很好,他详细说明了问题所在,即如果要进行透视,则需要定义静态数量的属性。不过,我不确定窗口函数是否有必要,所以我想这样做:

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

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

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

你能提供这个虚拟数据所需的结果吗?这样我们就可以看到你想要达到的格式了?@Mureinik,当然。给我几分钟,我会编辑我的问题。@Mureinik,我已经编辑了我的问题。请使用适当的报告工具,而不是强制SQL按您想要的方式显示数据@你能看看我下面的答案吗?它适用于任意数量的属性,并以您上面给出的格式返回结果。我已经编辑了我的问题。请您再看一下。内存和分辨率是列名吗?您是指内存和分辨率吗?内存和分辨率都是创建表后插入的值/记录。是的,我知道这些是属性表的记录。顺便问一下,你想用哪种方式得到你的结果,测试1还是测试2。window函数是必需的,因为我们不能保证属性ID是什么。至少在定义中没有。我们只需要“前两个”,真的吗?
lb_属性表的存在似乎掩盖了这一点。问题底部的显示引用了特定属性,这些属性在维度中具有特定的
attributeId
值。我在问题中没有提到“前两个”。这不会创建单独的列。@PhilipDevine在我看来,这会产生完全符合要求的结果:“如何连接这些表以显示/显示具有相应属性的两个产品?”并且它适用于任意数量的属性,其他答案都没有accomplish@danb,我会调查的。我只是有点忙了两三天。