我有一个非常简单的查询,我正在尝试获取计数。
const query = Product.query()
.withGraphJoined('collections')
.whereIn('collections.id', [1,3]);我尝试了resultSize(),它可以处理我进行的其他不使用withGraphJoined的查询。唯一有效的方法是对查询执行count(),这将返回以下内容:
Product {
'count(*)': 6,
id: 1,
name: 'product',
slug: 'slug',
price: 32,
}有没有一种更干净的方法来使用withGraphJoined对查询进行总计数?
发布于 2020-06-13 21:32:55
您正在尝试计算与该产品关联的集合的数量?你可以这样做:
const query = Product.query()
.whereIn('id', [1,3])
.select([
Product.ref('*'),
Product.relatedQuery('collections').count().as('collectionsCount')
])https://stackoverflow.com/questions/62220152
复制相似问题