【发布时间】:2020-01-19 04:30:55
【问题描述】:
我有两张表,如下图:
国家
id name
1 A
2 b
3 c
状态
id | country_id | name | population
1 | 1 | x | 234354
2 | 1 | y | 2334
3 | 2 | h | 232323
4 | 2 | E | 8238787
现在我想用这样的国家名称查询总人口数:
a has xxxx population
b has xxxx population
c has 0 population
在 django 查询中,我写了这个查询:
City.objects.values('country__name').annotate(Sum('population'))
但是对于 c 国家,这并没有显示 0 :(
【问题讨论】:
-
City.objects.exclude(country__isnull=True).values('country__name').annotate(sum=Sum('population')).order_by('-sum')