MYSQL - 如何分组并取非空值

标签 mysql sql

我有一个看起来像这样的表:

Name  | Color | Fruit | Animal
-----------------------------
Steve | Blue  | Null  | Null
Steve | Null  | Apple | Null
Steve | Null  | Null  | Sheep
John  | Red   | Apple | Null
John  | Null  | Null  | Cow

等...

我要它凝结成

Name  | Color | Fruit | Animal
------------------------------
Steve | Blue  | Apple | Sheep
John  | Red   | Apple | Cow

我尝试过的其他分组解决方案在 Null 上效果不佳。

最佳答案

可以使用max和group by

SELECT t.name,
       max(t.color) as color,
       max(t.fruit) as fruit,
       max(t.animal) as animal)
FROM YourTable
GROUP BY t.name

关于MYSQL - 如何分组并取非空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35484569/

相关文章:

php - CakePHP - 代码 $hasOne 的结果

sql - 在 PostgreSQL 上编写触发器脚本时出错

mysql - SQL - 查询关联 3 个不同的表和一个弱实体

php - Ajax调用面临尴尬局面

php - 确定邮政编码邻近度/范围的 MySQL 函数

mysql - 使用变量 mysql 无法按预期工作?

MYSQL 枢轴关键字搜索

mysql - 我应该使用哪个连接来执行此 mysql 查询?

mysql - 使用 SSMS 将数据从 MySql 传输到 SQL Server

c# - 在打开表单之前设置所有标签字体