mysql if() 与 max() [英] mysql if() with max()

查看:109
本文介绍了mysql if() 与 max()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 attproduct 的表:

I have a table named attproduct:

共有三列id、attribute、values.

我为 attribute 列中的每个 idvalue 中的相应值设置了 colorbrand

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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆