Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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 - Fatal编程技术网

mysql-如何检索组的所有属性,以及获取与组关联的给定产品的属性值

mysql-如何检索组的所有属性,以及获取与组关联的给定产品的属性值,mysql,sql,database,Mysql,Sql,Database,如何检索组的所有属性,例如,其中idGroup='2'和idLanguage='1' 然后对一些产品获取attrValue(如果存在),例如group='2'如果不存在,打印属性值 e、 g 我的桌子 表格:产品 idProduct idGroup 表格:产品属性 idProduct idAttr 闲置语言 属性值 表格:属性 idAttr 名称属性 闲置语言 表格:组属性 idGroup idAttr 有了这个疑问 结果是 如何连接表产品属性以获得这样的结果 Array (

如何检索组的所有属性,例如,其中idGroup='2'和idLanguage='1'
然后对一些产品获取attrValue(如果存在),例如group='2'如果不存在,打印属性值


e、 g

我的桌子 表格:产品

  • idProduct
  • idGroup
表格:产品属性

  • idProduct
  • idAttr
  • 闲置语言
  • 属性值
表格:属性

  • idAttr
  • 名称属性
  • 闲置语言
表格:组属性

  • idGroup
  • idAttr

有了这个疑问

结果是

如何连接表产品属性以获得这样的结果

Array
(
    [0] => Array (
            [idAttr] => 1
            [idGroup] => 1
            [nameAttr] => color
            [idProduct] => 1
            [valueAttr] => NULL or ''
        )
    [1] => Array (
            [idAttr] => 2
            [idGroup] => 1
            [nameAttr] => height
            [idProduct] => 1
            [valueAttr] => 1600
        )
    [2] => Array (
            [idAttr] => 3
            [idGroup] => 1
            [nameAttr] => width
            [idProduct] => 1
            [valueAttr] => 900
        )
)       
我仅在设置属性值时保存产品属性(例如,未为产品设置属性
color


编辑产品数据时,如何检索组的所有属性并获取属性值​​对于与组关联的给定产品?

请尝试使用下面的左外部联接和case语句(您必须确保表之间的联接条件正确)。我没有将products表包括在join中,但是如果您也想加入products,那么您也可以将outer join保留在其中

SELECT a.idAttr, a.nameAttr, case when pa.idProduct is not null then pa.attrValue else "" end
FROM attributes  AS a
INNER JOIN groups_attributes AS ga ON a.idAttr= ga.idAttr
LEFT OUTER JOIN products_attributes AS pa ON a.idAttr=pa.idAttr
WHERE ga.idGroup = 1 AND ga.idLanguage = eng

这是已加入产品的查询,如果希望“”而不是null,请使用
coalesce(pa.attrValue,')作为attrValue
而不是
pa.attrValue

SELECT a.idAttr, ga.idGroup, a.nameAttr, p.idProduct, pa.attrValue
FROM attributes  AS a
INNER JOIN groups_attributes AS ga ON a.idAttr= ga.idAttr
INNER JOIN products AS p ON ga.idGroup = p.idGroup
LEFT JOIN products_attributes pa ON p.idProduct = pa.idProduct AND pa.idAttr = a.idAttr and pa.idLanguage = ga.idLanguage 
WHERE ga.idGroup = 1 AND ga.idLanguage = eng
SELECT a.idAttr, a.nameAttr, case when pa.idProduct is not null then pa.attrValue else "" end
FROM attributes  AS a
INNER JOIN groups_attributes AS ga ON a.idAttr= ga.idAttr
LEFT OUTER JOIN products_attributes AS pa ON a.idAttr=pa.idAttr
WHERE ga.idGroup = 1 AND ga.idLanguage = eng
SELECT a.idAttr, ga.idGroup, a.nameAttr, p.idProduct, pa.attrValue
FROM attributes  AS a
INNER JOIN groups_attributes AS ga ON a.idAttr= ga.idAttr
INNER JOIN products AS p ON ga.idGroup = p.idGroup
LEFT JOIN products_attributes pa ON p.idProduct = pa.idProduct AND pa.idAttr = a.idAttr and pa.idLanguage = ga.idLanguage 
WHERE ga.idGroup = 1 AND ga.idLanguage = eng