我有一个我正在开发的期刊系统。 有一些表格:
表1:问题
issueid | issue_title
1 | November 2016
表2:文章
articleid | issueid | article_title
1 | 1 | Yet another article title
表3:作者
authorid | firstname | lastname
1 | John | Doe
2 | Jack | Foe
表4:文章作者
authorarticleid | authorid | articleid
1 | 1 | 1
2 | 2 | 1
表格articlesauthors
包含作者创作哪篇文章的记录。
现在,我需要获取作者的所有文章以解决特定问题。它应该是这样的,但无法弄清楚,正如我所提到的那样:
SELECT i.issue_title, ar.article_title, au.firstname, au.lastname
FROM articles ar
INNER JOIN articlesauthors aa1 ON aa1.articleid = ar.articleid
INNER JOIN articlesauthors aa2 ON aa2.articleid = au.authorid
INNER JOIN issues i ON i.issueid = ar.issueid
WHERE i.issueid = 1
好吧,我被困在这里,我不知道查询中类型authors
表的位置。
所需的输出应该是这样的:
issue_title | article_title | firstname | lastname
November 2... | Yet anothe... | John | Doe
November 2... | Yet anothe... | Jack | Foe
答案 0 :(得分:1)
您还可以检查修改后的SQL查询:
SELECT ar.article_title, aut.firstname, aut.lastname
FROM issues as issue
INNER JOIN articles as ar ON ar.issueid = issue.issueid
INNER JOIN articlesauthors as aa ON ar.articleid = aa.articleid
INNER JOIN authors as aut ON aut.authorid = aa.authorid
WHERE issue.issueid = 1
这将产生预期的输出。
答案 1 :(得分:0)
根据您发布的查询,您只使用三个表。你想获得auther名字和姓氏,但你没有加入auther表(你选择的是au.firstname,au.lastname但au id未定义)..尝试使用以下查询
select article_title,firstname,lastname from articles
inner join articlesauthors on articlesauthors.articleid=articles.articleid
inner join auther on auther.authorid=articlesauthors.authorid
inner join issue on issue.issueid=articles.issueid
答案 2 :(得分:0)
好好想几分钟就给我带来了解决方案。
SELECT i.issue_title, ar.article_title, au.firstname, au.lastname
FROM articles ar
INNER JOIN articlesauthors aa ON aa.articleid = ar.articleid
INNER JOIN authors au ON aa.authorid = au.authorid
INNER JOIN issues i ON i.issueid = ar.issueid
WHERE i.issueid = 1
现在这个查询为我带来了正确的数据集。