简化嵌套查询
Simplify nested queries
select name
from person, author, article
where name != "John Doe" AND
person.pid = author.pid AND
author.aid = article.aid AND
title = select title
from author, person, article
where person.name = "John Doe" AND
author.pid = person.pid AND
article.aid = author.aid
以上是我在 sqlite 中为关系代数编写的嵌套查询,该查询输出与 John Doe 合着一篇文章的人的姓名。
这是关系模式:
文章(aid, title, year, confid, numpages) Author(aid, pid) Person(pid, name, affiliation).
我的问题是如何简化嵌套查询?
我完全不明白你的问题与正则表达式有什么关系。不过,如果您想要 co-authored 与 John Doe 的人名,我建议加入:
select distinct pe2.name
from person pe1
inner join author au1 on au1.pid = pe1.pid
inner join author au2 on au2.aid = au1.aid and au2.pid <> au1.pid
inner join person pe2 on pe2.pid = au2.pid
where pe1.name = 'John Doe'
查询从 John Doe 开始,并带入 author
中的相应行;然后,它搜索所有 co-authors,最后找到他们的名字。
select name
from person, author, article
where name != "John Doe" AND
person.pid = author.pid AND
author.aid = article.aid AND
title = select title
from author, person, article
where person.name = "John Doe" AND
author.pid = person.pid AND
article.aid = author.aid
以上是我在 sqlite 中为关系代数编写的嵌套查询,该查询输出与 John Doe 合着一篇文章的人的姓名。
这是关系模式:
文章(aid, title, year, confid, numpages) Author(aid, pid) Person(pid, name, affiliation).
我的问题是如何简化嵌套查询?
我完全不明白你的问题与正则表达式有什么关系。不过,如果您想要 co-authored 与 John Doe 的人名,我建议加入:
select distinct pe2.name
from person pe1
inner join author au1 on au1.pid = pe1.pid
inner join author au2 on au2.aid = au1.aid and au2.pid <> au1.pid
inner join person pe2 on pe2.pid = au2.pid
where pe1.name = 'John Doe'
查询从 John Doe 开始,并带入 author
中的相应行;然后,它搜索所有 co-authors,最后找到他们的名字。