gpt4 book ai didi

MySQL - 如果没有则返回空值

转载 作者:搜寻专家 更新时间:2023-10-30 22:33:51 25 4
gpt4 key购买 nike

我在 MySQL 中有以下模式(只读权限数据库)

http://sqlfiddle.com/#!9/1eafc/1

如您所见,只有 5 个国家/地区代码:

GB, USA, GR, ES, DE

某些月份的某些记录可能不包含其中一个国家/地区,因为该月没有为该国家/地区创建记录。

如您在 sqlfiddle 中所见,GB 记录仅在 9 月创建。

如果那个月没有某个国家的任何记录,有没有办法显示 0 或 NULL 之类的,而不是不返回?

到目前为止,我已经尝试了 ISNULLIFNULLCOALESCE 的不同变体,但都没有奏效。

我想返回如下内容

UK 0
USA 13
GR 5
ES 12
DE 1

有什么想法吗?

最佳答案

好的,所以我将做一些假设来代替功能性 SQLFiddle。国家代码与分组数据存在于同一个表中。

如果是这样,为什么不使用:

select DT1.CountryCode, count(DT2.ValueForCounting) as ReturnedCount
from (
select distinct CountryCode
from DataTable ) DT1
left join DataTable DT2
on DT1.CountryCode = DT2.CountryCode
and DT2.QueryConditions = 'Stuff' -- Use the join condition instead of a where clause for anything to do with DT2

关于MySQL - 如果没有则返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39766062/

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