gpt4 book ai didi

mysql - 我如何计算状态

转载 作者:行者123 更新时间:2023-11-29 21:54:00 24 4
gpt4 key购买 nike

我有这个表结构

CREATE TABLE `table_detail` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` INT(11) NOT NULL,
`status` CHAR(2) NOT NULL,
`dateTime_updated` DATETIME NOT NULL,
PRIMARY KEY (`id`),

)
COLLATE='utf8_general_ci'
ENGINE=InnoDB

;

我想统计指定的用户状态为 7,6 和 4查询 2015-09-07 06:00:00 - 2015-09-11 20:00:00

的日期时间

7=事件、6=非事件、4=已删除

预期输出

+---------+--------+----------+---------+
| user_id | active | inactive | deleted |
+---------+--------+----------+---------+
| 55 | 2 | 3 | 0 |
| 26 | 4 | 6 | 3 |
| 15 | 3 | 7 | 7 |
| 24 | 6 | 3 | 3 |
+---------+--------+----------+---------+

提前谢谢您。

最佳答案

您可以为此使用条件聚合。您需要做的就是在 select 子句中添加 SUM(condition) 列,并按 user_id 值进行分组,如下所示:

SELECT user_id, 
SUM(status = 7) AS active,
SUM(status = 6) AS inactive,
SUM(status = 4) AS deleted
FROM table_detail
WHERE dateTime_updated BETWEEN '2015-09-07 06:00:00' AND '2015-09-11 20:00:00'
GROUP BY user_id;

关于mysql - 我如何计算状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33359346/

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