繁体   English   中英

查询连续两年发表论文的作者ID

[英]Query to get the ID of authors who published papers in two consecutive years

我想写一个查询,显示连续两年发表论文的作者 ID。 这是数据库架构:

CREATE TABLE Author (aid integer NOT NULL,
name varchar(50) NOT NULL,
affiliation varchar(50), primary key(aid));

CREATE TABLE Paper (pid  integer NOT NULL,
title varchar(50) NOT NULL,
year integer NOT NULL, primary key(pid));

CREATE TABLE Authored (aid integer references Author,
pid integer references Paper,
primary key(aid, pid), foreign key(aid) references Author(aid), foreign key(pid) references Paper(pid));

insert into Author(aid, name, affiliation) values (1, "A", "DS");
insert into Author(aid, name, affiliation) values (2, "B", "PS");
insert into Author(aid, name, affiliation) values (3, "C", "CS");

insert into Paper(pid, title, year) values (100, "DS1", 2019);
insert into Paper(pid, title, year) values (101, "PS1", 2019);
insert into Paper(pid, title, year) values (102, "CS1", 2019);
insert into Paper(pid, title, year) values (103, "DS2", 2020);
insert into Paper(pid, title, year) values (104, "PS2", 2020);
insert into Paper(pid, title, year) values (105, "CS2", 2019);

Authored.aid 是 Author 的外键,Authored.pid 是 Paper 的外键。 这就是我到目前为止所拥有的,但这并没有给我想要的结果,我不知道出了什么问题:

select au1.aid, a1.name
from authored au1 inner join authored au2 on au1.aid=au2.aid
    inner join author a1 on au1.aid=a1.aid
    inner join paper p1 on au1.pid=p1.pid
    inner join paper p2 on au2.pid=p2.pid
where p1.year = p2.year+1
order by au1.aid;

不确定您的查询,但 IN 子句看起来很有用:

    where paper.year IN ('2019', '2020') 

您需要GROUP BY au1.aid以获得不同的作者:

SELECT au1.aid, a1.name
FROM authored au1 INNER JOIN authored au2 ON au1.aid=au2.aid 
    INNER JOIN author a1 ON au1.aid=a1.aid
    INNER JOIN paper p1 ON au1.pid=p1.pid
    INNER JOIN paper p2 ON au2.pid=p2.pid AND p1.year+1 = p2.year
GROUP BY au1.aid
ORDER BY au1.aid

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM