gpt4 book ai didi

MYSQL - 如何分组并取非空值

转载 作者:行者123 更新时间:2023-11-29 06:16:42 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com