PostgreSQL
 sql >> Base de données >  >> RDS >> PostgreSQL

Trouver tous les co-auteurs - Facettage/Regroupement pour plusieurs tables de mappage

Essayez ceci :

SELECT "AuthorId", COUNT(*)
FROM BookAuthorMapping
WHERE "BookId" IN (SELECT "BookId" FROM BookAuthorMapping WHERE "AuthorId" = 1)
GROUP BY "AuthorId"

Démo ici

Vous pouvez également utiliser un INNER JOIN :

SELECT t1."AuthorId", COUNT(*)
FROM BookAuthorMapping AS t1
INNER JOIN BookAuthorMapping AS t2 ON t1."BookId" = t2."BookId" AND t2."AuthorId" = 1
GROUP BY t1."AuthorId"

Démo ici