gpt4 book ai didi

MySQL SELECT 基于第二个字段值

转载 作者:行者123 更新时间:2023-11-29 04:18:30 25 4
gpt4 key购买 nike

我想根据第二个字段的值重新组合我的结果。

我有下表:

SELECT case_id, current FROM progress

+---------+---------+
| case_id | current |
+---------+---------+
| 158 | 1 |
| 158 | 1 |
| 165 | 0 |
| 165 | 0 |
| 166 | 0 |
| 166 | 1 |
| 166 | 0 |
+---------+---------+

并且想像这样重新组合 case_id :

+---------+---------+
| case_id | current |
+---------+---------+
| 158 | 1 |
| 165 | 0 |
| 166 | 1 |
+---------+---------+

因此,如果只有“1”,则为“1”。如果只有'0',我们得到'0'。但是,如果我们有 2 个值但至少有一个“1”,那么它将是“1”。

我已经尝试使用 group 但它没有按我预期的那样工作。

我不知道最优化的方法是什么。

最佳答案

你所描述的听起来像max():

select case_id, max(current) as current
from t
group by case_id;

关于MySQL SELECT 基于第二个字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33213999/

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