gpt4 book ai didi

mysql - 合并mysql中id的计数总和

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

我不是mysql专家,我需要帮助来进行计数查询,我需要将id 5和11的计数总和合并到一个计数数字= 286,并在本例中将平台名称指定为GCS。

   SELECT DISTINCT (p.id) AS id, (p.name) AS platform,
IFNULL(count(e.id), 0) AS count
FROM event e, lu_platform p
WHERE e.platform_id = p.id
AND p.id NOT IN ( 10, 15, 17, 18 )
AND e.sourcetype_id = 1
AND e.event_datetime BETWEEN '2013-11-4'
AND '2013-11-10' AND e.sender_id NOT IN ( 759, 73 )
GROUP BY p.id ORDER BY id;

+----+---------------------------+-------+
| id | platform | count |
+----+---------------------------+-------+
| 3 | GGG | 414 |
| 4 | KIKI | 156 |
| 5 | KJC | 284 |
| 6 | LOLO | 4 |
| 7 | MOD | 1147 |
| 8 | MARKT | 1049 |
| 11 | GCS | 2 |
| 12 | POLAR | 30 |
| 14 | GUAE | 145 |
+----+---------------------------+-------+

最佳答案

一种可能的方法 - 使用子查询、求和、
和 IF 函数或 case 表达式 CASE WHEN THEN:

SELECT sum( case when id in ( 5, 11 ) then count else 0 end ) as count_5_11,
sum( if( id in ( 3, 4 ), count, 0 ) ) As count_3_4
FROM (
-- your query goes here
SELECT DISTINCT (p.id) AS id, (p.name) AS platform,
IFNULL(count(e.id), 0) AS count
....
....
....
....
....
) AS some_alias

关于mysql - 合并mysql中id的计数总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20781183/

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