如何编写SQL查询以查找那些共同撰写了一些论文的作者。
架构:
Authors (
authorID,
firstName,
);
Papers (
paperID,
title,
);
PaperbyAuthor (
authorID FOREIGN KEY REFERENCES Authors,
paperID FOREIGN KEY REFERENCES Papers
);
答案 0 :(得分:2)
这将列出所有作者及其PaperID一起撰写论文的作者。
SELECT PA.paperID, GROUP_CONCAT(A.firstName separator ',') FROM PaperbyAuthor PA
INNER JOIN Authors A ON PA.authorID = A.authorID
WHERE paperID IN (SELECT paperID FROM PaperbyAuthor PA
GROUP BY paperID
HAVING COUNT(*) > 1)
如果您需要查看论文标题,也可以使用论文加入表格。
答案 1 :(得分:1)
这会给你每一对作者两次:如果约翰和乔共同撰写一篇论文,那么会有参赛作品
CoAuthor1 CoAuthor2 JointPapers
JOHN JOE 1
JOE JOHN 1
代码是:
SELECT
a1.firstName AS CoAuthor1,
a2.firstName AS CoAuthor2,
l.JointPapers
FROM
(
SELECT
pba1.authorID AS CoAuthorID1,
pba2.authorID AS CoAuthorID2,
COUNT(*) AS JointPapers
FROM
PaperByAuthor AS pba1
LEFT JOIN
PaperByAuthor AS pba2
ON
pba1.paperID = pba2.paperID
GROUP BY
pba1.authorID,
pba2.authorID
) AS l
LEFT JOIN
Authors AS a1
ON
l.CoAuthorID1 = a1.authorID
LEFT JOIN
Authors AS a2
ON
l.CoAuthorID2 = a2.authorID
答案 2 :(得分:0)
如果您想要共同撰写特定论文的作者,请尝试:
SELECT a.authorID, a.firstName
FROM Authors a
INNER JOIN PaperbyAuthor pa ON pa.authorID = a.authorID
INNER JOIN Papers p ON p.paperID = pa.paperID
WHERE p.paperID = 1000;
将值1000更改为所需纸张的ID。如果要按标题查找论文,请将最后一行更改为:
WHERE p.title = 'The Best Paper';
答案 3 :(得分:0)
您只是在寻找作者对吗?
select distinct min(authorID), max(authorID)
from PapersByAuthor pa
group by paperID
having count(authorID) = 2