Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.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 if()与max()的比较_Mysql_Entity Attribute Value - Fatal编程技术网

mysql if()与max()的比较

mysql if()与max()的比较,mysql,entity-attribute-value,Mysql,Entity Attribute Value,我有一个名为attproduct的表: 有三列id、attribute、values 我在属性列中为每个id设置了颜色和品牌,并在值列中设置了相应的值 SELECT id, MAX( IF( attribute = 'brand', value, NULL ) ) AS Brand, MAX( IF( attribute = 'color', value, NULL ) ) AS color FROM fy.attproduct GROUP BY id 当我运行此查询时,我在

我有一个名为
attproduct
的表:

有三列
id、attribute、values

我在
属性
列中为每个
id
设置了
颜色
品牌
,并在
列中设置了相应的值

SELECT id, MAX( IF( attribute =  'brand', value, NULL ) ) AS Brand,
       MAX( IF( attribute =  'color', value, NULL ) ) AS color
FROM fy.attproduct
GROUP BY id
当我运行此查询时,我在
id、brand、color
列中获得所需的输出


我需要知道max在我的查询中扮演什么角色,当我删除max时,我会得到空值

group by id
将attproduct中与给定id关联的所有行滚动到一个结果行中。如果未指定聚合器(如min或max),则将从源行中为结果选择一个随机值(可能是第一个找到的值)。

max()
将组合与每个id关联的值

SELECT id, IF( attribute =  'brand', value, NULL ) AS Brand, IF( attribute =  'color', value, NULL ) AS color
FROM fy.attproduct
如果不使用
,groupby
应返回如下行

ID  Brand      color
1   'mybrand'  NULL
1   NULL       'mycolor'
当未使用
MAX()
时,将只选择一行,因此至少有一列为
NULL