gpt4 book ai didi

mysql - 一个查询中的嵌套计数

转载 作者:行者123 更新时间:2023-11-29 05:26:16 24 4
gpt4 key购买 nike

我需要计算每种类型的消息表中未读消息的数量。

:消息

字段:

Id Message Type Unread
1 xxxxx 0 0
2 xxxxx 1 0
3 xxxxx 1 0
4 xxxxx 1 1
5 xxxxx 2 0
6 xxxxx 3 0
7 xxxxx 3 1

所以,我需要这样的结果:

For type 0 there is 1 unread message, and a total of 1 message.
For type 1 there are 2 unread messages, and a total of 3 messages.
For type 2 there is 1 unread message, and a total of 1 message.
For type 3 there is 1 unread message, and a total of 2 messages.

到目前为止,我能够计算每种类型的消息数量:

SELECT 
`message_type`,
COUNT(`message_type`) AS message_type_count
FROM
Messages
GROUP BY `message_type`

但是,我还需要每种类型的未读邮件数量。我该怎么做?

感谢您的帮助!

最佳答案

SELECT message_type,
COUNT(message_type) AS message_type_count,
SUM(Unread = 1) AS unread_count
FROM Messages
GROUP BY message_type

关于mysql - 一个查询中的嵌套计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19912538/

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