我有一个名为 attproduct 的表:
I have a table named attproduct:
共有三列id、attribute、values.
我为 attribute 列中的每个 id 和 value 中的相应值设置了 color 和 brand 列
I have color and brand for each id in attribute column and corresponding values in value column
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 中获得所需的输出作为列.
When I run this query i get output as desired in id, brand, color as columns.
我需要知道 max 在我的查询中的作用是什么,当我删除 max 时,我得到空值
I need to know what is role of max in my query, when i remove max, i get null values
推荐答案MAX() 正在组合与每个 id 关联的值.
MAX() is combining the values associated with each id.
SELECT id, IF( attribute = 'brand', value, NULL ) AS Brand, IF( attribute = 'color', value, NULL ) AS color FROM fy.attproduct没有 GROUP BY 应该返回像
ID Brand color 1 'mybrand' NULL 1 NULL 'mycolor'当不使用MAX()时,只会选择其中一行,所以至少有一列是NULL.
When MAX() is not used, only one of the rows will be chosen, so at least one column will be NULL.
更多推荐
mysql if() 与 max()
发布评论