sql - 查找所有共同作者 - 多对多映射表的分面/分组

标签 sql postgresql join group-by facet

Table : Books 
-------------
ID    Name
1     Book1
2     Book2
3     Book3
4     Book4

Table : Authors 
-------------
ID    Name
1     Author1
2     Author2
3     Author3
4     Author4


Table :  BookAuthorMapping
---------------------------
ID    BookId    AuthorId
1     1         1
2     1         2
3     1         3
4     2         2
5     2         3
6     3         3
7     4         4  

所以,

  • Book1 由 Author1,Author2,Author3 撰写
  • Book2 由 Author2,Author3 撰写
  • Book3 仅由作者 3 撰写
  • Book4 仅由作者 4 撰写

问题是: 给定一个 AuthorId,我需要找出其他作者与他合着了多少本书:

示例:

Given AuthorId: 1
-------------------
AuthorId   Count 
2          1           // 2 has co-authored only book1
3          1           // 3 has co-authored only book1
1          1           // Its okay, if i get author1 in the result

Given AuthorId: 2
-------------------
AuthorId   Count 
1          1           // 1 has co-authored only book1
3          2           // 3 has co-authored book1 and book2
2          2           // Its okay if i get author 2 in the result

Given AuthorId: 3
-------------------
AuthorId   Count 
1          1          // 1 has co-authored only book1
2          2          // 2 has co-authored book1 and 2
3          3          // Its okay if i get author 3 in the result.

Given AuthorId: 4
-------------------
AuthorId   Count 
4          1     // I should not get author1 0 author2 0 , author3 0 for this

-- 编辑-- 我有一个类似的解决方案:select aId, count(mapping.bId) from mapping join (select bId from mapping where aId = ?) as tmp on mapping.bId = tmp.bId group by aId;

另外,@Giorgos Betsos 在回复中提到了同样的内容。

我很好奇如果没有内部查询是否可行。

最佳答案

试试这个:

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

Demo here

您也可以使用 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"

Demo here

关于sql - 查找所有共同作者 - 多对多映射表的分面/分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35093560/

相关文章:

sql - 如何一次删除所有ms-access表中的数据?

php - 得到奇怪的结果 : mysqli_fetch_array() expects parameter 1 to be mysqli_result

SQL Server 将字符串转换为日期时间

ruby-on-rails - Rails 数据库索引 : Is this an anti-pattern?

mysql - 超过 2GB 限制的 SQL Server Express 替代方案

sql - 使用 pgrouting 需要 Lat,long 形式的路径

mysql - 使用 WHERE 时在 SQL Join 中包含 NULL

postgresql - 用 for 循环替换多个左连接

sql - Postgres : how to call a function that returns a table and pass params from a query

MYSQL 获取最新购买的用户