mysql - 获取未读消息的计数
问题描述
我对我们的数据库有这个要求,如下所示
我想获得 3 列requestType
,CategoryGroup
和readStatus
列数
结果将requestType
一直显示categoryGroup
,如果readStatus
是假的,那么它将显示它的计数,如果所有readStatus
都是真的,requestType
那么返回 0。
同样,如果requestType
isNULL
和 Blank 相同categoryGroup
(来自表 A,subcategoryId 12 和 13 来自categoryGroup
表 B 中的相同),则 categoryGroup 应显示为 BLANK 并应保存 ( readStatus=0
) NULL 和 BLANK的计数
我写的查询只返回列和计数,readStatus=false
并显示它的总计数。
select coalesce( m.requestType, "") as requestType,
count(m.messageId) as count,
s.categoryGroup categoryGroup
from mb_message m
join mb_subcategory s on m.subCategoryId = s.subCategoryId
where m.readStatus=0
and m.storeId = ?
and countryCode= ?
and m.modifiedDate >= ?
group by m.requestType, m.subCategoryId;
上述查询的预期结果是:
解决方案
我会做类似的事情:
select
requesttype,
sum(cnt) as cnt,
categorygroup
from (
select
case when a.requesttype is null or a.requesttype = ''
then 'BLANK' else a.requesttype end as requesttype,
case when a.readstatus = 0 then 1 else 0 end as cnt,
b.categorygroup
from tablea a
join tableb b on b.subcategoryid = a.subcategoryid
) x
group by requesttype, categorygroup
推荐阅读
- c++ - TaskScheduler-Library 的预处理不起作用 - 多定义错误
- android - Android P 阻止了相机访问
- android - 使用 Android Studio 颤振不显示设备列表
- r - 从网页到 R 的阅读表
- javascript - 如何将html符号从javascript发送到java
- java - 将 ByteBuffer 的内容写入 DataOutput?
- postgresql - Postgres:10 在 docker swarm 集群中。数据库系统已关闭
- java - 独立 Wildfly 的微配置文件实现
- javascript - javascript中的多个构造函数(重载)
- python - 如何在 Conv2D 层 Tensorflow 中获得步幅值?