mysql if() 与 max()

编程入门 行业动态 更新时间:2024-10-12 03:20:29
本文介绍了mysql if() 与 max()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个名为 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()

本文发布于:2023-11-22 01:14:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615397.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mysql   max

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!