代码之家  ›  专栏  ›  技术社区  ›  Norbert Pushparaj Yuvaraj

从异议中获得计数。使用withGraphJoined的js查询

  •  0
  • Norbert Pushparaj Yuvaraj  · 技术社区  · 6 年前

    我有一个非常简单的查询,我正在尝试获取计数。

    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,
    }
    

    有没有更干净的方法来对查询进行总计 用图形连接 ?

    0 回复  |  直到 6 年前
        1
  •  2
  •   user6269972    6 年前

    您正在尝试统计与产品相关的集合?你可以这样做:

    const query = Product.query()
      .whereIn('id', [1,3])
      .select([
         Product.ref('*'),
         Product.relatedQuery('collections').count().as('collectionsCount')
      ])