Select select subquery with Knex.js

I am trying to create the following query with subqueries using Knex:

SELECT 
  t.*, 
  (SELECT COUNT(*) FROM team_users tu WHERE TeamID = t.ID) AS UserCount,
  (SELECT COUNT(*) FROM team_access ta WHERE TeamID = t.ID) AS AppCount
FROM teams t WHERE OwnerUserID = _UserID;

The result should be a table of commands with aggregation of UserCount and AppCount counters from different tables (team_users, team_access)

id | Name      | OwnerUserID   | UserCount | AppCount
-----------------------------------------------------
134| Team A    | 1538          | 7         | 6
135| Team B    | 1538          | 4         | 2
136| Team C    | 1538          | 12        | 1

What I understood as an equivalent knex implementation was:

var subquery1 = Knex.knex('team_users').count('*').where('TeamID', 'teams.ID').as('UserCount');
var subquery2 = Knex.knex('team_access').count('*').where('TeamID', 'teams.ID').as('AppCount');
Knex.knex.select('*', subquery1, subquery2).from('teams').where("OwnerUserID", ownerId).asCallback(dataSetCallback);

By running this, I get the "UserCount" and "AppCount" columns in the returned object, but always as zero, possibly because it does not identify "teams.ID" in the subquery.

I was able to solve this problem using the Knex.raw function:

Knex.knex('teams')
    .select('*', Knex.knex.raw('(SELECT COUNT(*) FROM team_users WHERE TeamID = teams.ID) AS UserCount'), Knex.knex.raw('(SELECT COUNT(*) FROM team_access WHERE TeamID = teams.ID) AS AppCount'))
    .where("OwnerUserID", ownerId)
    .asCallback(dataSetCallback);

but I'm curious to know how to achieve this using subquery objects.

+6
source share
1 answer

teams.ID . .where('columnName', 'otherColumnName'), knex.ref otherColumnName .

var teamsIdColumnIdentifier = knex.ref('teams.ID'); // <-- [1]

var subquery1 = Knex.knex('team_users').count('*')
  .where('TeamID', teamsIdColumnIdentifier).as('UserCount');
var subquery2 = Knex.knex('team_access').count('*')
  .where('TeamID', teamsIdColumnIdentifier).as('AppCount');

Knex.knex.select('*', subquery1, subquery2).from('teams')
  .where("OwnerUserID", ownerId).asCallback(dataSetCallback);

[1] , knex.ref Knex 2018 , knex.raw, ;

var teamsIdColumnIdentifier = knex.raw('??', ['teams.ID']);
+6

Source: https://habr.com/ru/post/1670412/


All Articles