gpt4 book ai didi

mysql - 按类型分组,但将 2 种类型作为 1 种?

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:13 26 4
gpt4 key购买 nike

我不知道如何给这个问题起标题。

我的数据库中有一张表,看起来像这样:

[id][name][type]
1-John-2
2-Jack-3
3-Liam-1
4-Kim-1
5-Michael-3
And many more

我想制作一个按类型分组的列表,但是类型 1 和类型 2 是一组,就像这样:

[Type 1 and 2]
1 - John
3 - Liam
4 - Kim
[Type 3]
2 - Jack

在 MySQL 中可以这样分组吗?

最佳答案

是的,你可以用例

select *,
case when `type` =2 then 1 else `type` end new_type
from t
order by `type` ;

聚合函数

select count(*)
,case when `type` =2 then 1 else `type` end new_type
from t
group by new_type

Demo

关于mysql - 按类型分组,但将 2 种类型作为 1 种?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24925859/

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