gpt4 book ai didi

mysql - mysql如何合并列的值

转载 作者:行者123 更新时间:2023-11-30 01:11:51 26 4
gpt4 key购买 nike

让我们考虑这个查询

select class_id,case when event_id=2 then sum(time_spent) end as timespent ,case when event_id=3 then sum(timespent) end as visitedtimespent from class group by class_id,event_id;

输出看起来像

class_id     timespent visitedtimespent

1 2000 NULL
1 NULL 10
2 4000 NULL
2 NULL 5

当我使用此查询时

select class_id,case when event_id=2 then sum(time_spent) end as timespent ,case when event_id=3 then sum(time_spent) end as timespent from class group by class_id;

输出看起来像

class_id     timespent visitedtimespent

1 2000 NULL
2 4000 NULL

但我期望这个输出

class_id     timespent visitedtimespent

1 2000 10
2 4000 5

我怎样才能实现这个目标?

最佳答案

select class_id,
sum(case when event_id=2 then time_spent else 0 end) as timespent,
sum(case when event_id=3 then time_spent else 0 end) as visitedtimespent
from class
group by class_id

关于mysql - mysql如何合并列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19397730/

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