【问题标题】:django left join and right join implement sqlitedjango left join 和 right join 实现 sqlite
【发布时间】: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')

标签: django model


【解决方案1】:

查询未显示 c 国家/地区的任何记录,因为表没有 c 国家/地区的任何记录。

City.objects.values('country__name').annotate(Sum('population'))

此查询将显示城市模型中的所有记录。

【讨论】:

  • 任何原始查询?我们可以用这个吗?
猜你喜欢
  • 2013-05-02
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 2020-03-07
相关资源
最近更新 更多