【问题标题】:Proving relational calculus with SQL用 SQL 证明关系演算
【发布时间】:2016-02-28 02:23:23
【问题描述】:

对于一个作业,我的任务是为此语句编写一个关系微积分查询,

哪位作者与每位作者共同撰写了至少 1 篇论文(没有聚合函数)

鉴于此架构,

Authors( auId, name)
Authoring( articleId, authorId )

我想首先使用 sql 来证明我的查询,但我注意到我只得到与每篇文章共同撰写的作者,而我只需要与其他作者共同撰写至少一篇文章的作者。如何更改我的查询以达到预期的结果?谢谢。

这是我想出的查询,

select distinct authorid
from authoring as au1
where not exists(
    select *
    from authoring as ar1
    where not exists(
        select *
        from  authoring as a2
        where a2.articleid = ar1.articleid
          and a2.authorid  = au1.authorid
));

The result I am getting is
authorId
--------
1

The result should be 1,2 because authorID 2 shares a article with both authors   1 & 3

authorId
--------
1
2

Authors Table

auid | name
-----------
1    | Michael
2    | Jazmine
3    | Amanda

Authoring Table
articleId | authorId
--------------------
1         | 1
2         | 1
3         | 1
1         | 2
3         | 2
1         | 1

我已经在下面发布了这个问题的答案。

【问题讨论】:

  • 您能否添加示例数据和预期结果。至少对我来说很难理解
  • 已添加示例数据

标签: mysql sql relational-algebra


【解决方案1】:

用于查找与其他作者共同撰写至少一篇文章的所有作者的 SQL 查询。

select *
from `authors` as a1
where not exists(
    select *
    from `authors` as a2
    where a2.auid <> a1.auid
    and not exists(
        select *
        from authoring as ar1
        inner join authoring as ar2
            on ar1.articleid = ar2.articleid
        where ar1.authorid    = a1.auid
        and   ar2.authorid    = a2.auid
));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多