gpt4 book ai didi

mysql - 按优先级分组

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

我有这个

+---------+--------+-------+
| article | name |status |
+---------+--------+-------+
| 0001 | A | enable|
| 0002 | A | temp |
| 0003 | B | enable|
| 0004 | C | enable|
+---------+--------+-------+

我想从此表“产品”中选择所有内容,但我想按名称分组,如果有状态 temp 我想忽略 enable 状态和仅显示具有 temp 状态

的产品

查询后的结果将是:

+---------+--------+-------+
| article | name |status |
+---------+--------+-------+
| 0002 | A | temp |
| 0003 | B | enable|
| 0004 | C | enable|
+---------+--------+-------+

你能帮我构建这个查询吗?

最佳答案

如果在组中找到至少一个温度,则显示它。否则显示启用

select article, name, if(sum(status='temp'), 'temp', 'enable') 
from thetable
group by name

要获取对应于临时状态的文章,使用这样的查询

select * from table1 
where status = 'temp'
union
select * from table1
where name not in (select distinct name from table1 where status = 'temp' )

关于mysql - 按优先级分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37656062/

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