在django中检测真布尔字段的正确方法是什么?

sczxawaw  于 2021-08-09  发布在  Java
关注(0)|答案(0)|浏览(381)

我想知道每个用户是否至少有一个ispinned=true。因此,对于下面的模式,我按用户id和计数分组(ispinned=true)。

Table STORY
-------------------
user_id    isPinned
0          True
0          True
0          False
1          True
1          False

此查询可获得半期望的输出:

Story.objects.values('user_id').annotate(pinnedCount=Count(Case(When(isPinned=True, then=1), output_field=IntegerField(),)))

[{'user_id': 0, 'isPinnedCount': 2}, 
 {'user_id': 1, 'isPinnedCount': 1}]

但是,我希望sql在第一次遇到真值后停止不必要的计数,以便更快地查询。
计数是否正确的数据库策略来检测每个用户是否至少有一个ispinned=true?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题