gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:46:05 25 4
gpt4 key购买 nike

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

<小时/>

例如

我的 table

表:产品

  • id产品
  • id组

表:products_attributes

  • id产品
  • idAttr
  • idLanguage
  • attr值

表:属性

  • idAttr
  • 名称属性
  • idLanguage

表:groups_attributes

  • id组
  • idAttr
<小时/>

使用此查询

SELECT a.idAttr, a.nameAttr
FROM attributes AS a
INNER JOIN groups_attributes AS ga ON a.idAttr= ga.idAttr
WHERE ga.idGroup = 1 AND ga.idLanguage = eng

结果是

Array
(
[0] => Array (
[idAttr] => 1
[idGroup] => 1
[nameAttr] => color
)
[1] => Array (
[idAttr] => 2
[idGroup] => 1
[nameAttr] => height
)
[2] => Array (
[idAttr] => 3
[idGroup] => 1
[nameAttr] => width
)
)

如何连接表products_attributes以获得这样的结果?

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
)
)

仅在设置属性值时才保存产品的属性(例如,未为产品idProduct=1设置属性color)

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

最佳答案

尝试使用左外连接和 case 语句,如下所示(您必须确保表之间的连接条件正确)。我没有在联接中包含产品表,但如果您也想联接产品,您也可以留下外部联接。

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

关于mysql - 如何检索组的所有属性并获取与该组关联的给定产品的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21355100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com