Skip to content
Advertisement

Get distinct members for an array of group IDs

I have 3 tables:

  1. Scorecard
  2. Group
  3. Scorecard_Group (Joint table)

I am trying to write a query that fulfills the following purpose:

  1. Get the list of distinct scorecard ids WHERE Scorecard_Group.groupId IN (Pass array of groupIds)
  2. Get all Scorecard.”name” where Scorecard.”id” IN (Array of scorecardIds that we just queries)

I am able to achieve this using 2 queries but I would like for an optimal solution. Here is my 2 queries solution:

I tried to achieve the same result in 1 query but I am getting:

Advertisement

Answer

Assuming a proper many-to-many implementation like:

Seems like you twisted / over-complicated your objective as well as your query.

This retrieves the full, distinct set of scorecards (id, name) that are member in the given groups (array of group_id’s).

Or, the same a bit faster for many duplicate memberships:

Using legal, lower-case, unquoted identifiers like you should, too. See:

To use IN, you’d have to provide a set or list.
Use = ANY for an array. See:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement