具有多个联接的 SQL 分组错误地组合结果

发布于 2024-08-27 05:45:43 字数 1836 浏览 7 评论 0原文

您好,我的查询在不应该合并记录时遇到了问题。

我有两个表 Authors 和 Publications,它们通过出版物 ID 以多对多关系相关。由于每个作者可以有许多出版物,并且每个出版物有许多作者。我希望我的查询返回一组作者的每一篇出版物,并包括对分组到一个字段中的出版物做出贡献的每个其他作者的 ID。 (我正在使用 mySQL)

我尝试在下面以图形方式描绘它

    Table: authors               Table:publications
AuthorID | PublicationID        PublicationID | PublicationName
    1    |   123                       123    |       A
    1    |   456                       456    |       B
    2    |   123                       789    |       C
    2    |   789
    3    |   123
    3    |   456

我希望我的结果集如下

 AuthorID | PublicationID | PublicationName | AllAuthors
     1    |       123     |        A        |    1,2,3
     1    |       456     |        B        |    1,3
     2    |       123     |        A        |    1,2,3
     2    |       789     |        C        |     2
     3    |       123     |        A        |    1,2,3
     3    |       456     |        B        |    1,3

这是我的查询

Select   Author1.AuthorID,
    Publications.PublicationID,
    Publications.PubName,
    GROUP_CONCAT(TRIM(Author2.AuthorID)ORDER BY Author2.AuthorID ASC)AS 'AuthorsAll'
FROM Authors AS Author1
LEFT JOIN Authors AS Author2
ON Author1.PublicationID = Author2.PublicationID
INNER JOIN Publications
ON Author1.PublicationID = Publications.PublicationID
WHERE Author1.AuthorID ="1" OR Author1.AuthorID ="2" OR Author1.AuthorID ="3" 
GROUP BY Author2.PublicationID

但它返回以下内容

 AuthorID | PublicationID | PublicationName | AllAuthors
     1    |       123     |        A        |    1,1,1,2,2,2,3,3,3
     1    |       456     |        B        |    1,1,3,3
     2    |       789     |        C        |     2

当 where 语句中只有一个 AuhorID 时,它确实提供了所需的输出。 我一直无法弄清楚,有谁知道我哪里出错了?

Hi I'm having trouble with my query combining records when it shouldn't.

I have two tables Authors and Publications, they are related by Publication ID in a many to many relationship. As each author can have many publications and each publication has many Authors. I want my query to return every publication for a set of authors and include the ID of each of the other authors that have contributed to the publication grouped into one field. (I am working with mySQL)

I have tried to picture it graphically below

    Table: authors               Table:publications
AuthorID | PublicationID        PublicationID | PublicationName
    1    |   123                       123    |       A
    1    |   456                       456    |       B
    2    |   123                       789    |       C
    2    |   789
    3    |   123
    3    |   456

I want my result set to be the following

 AuthorID | PublicationID | PublicationName | AllAuthors
     1    |       123     |        A        |    1,2,3
     1    |       456     |        B        |    1,3
     2    |       123     |        A        |    1,2,3
     2    |       789     |        C        |     2
     3    |       123     |        A        |    1,2,3
     3    |       456     |        B        |    1,3

This is my query

Select   Author1.AuthorID,
    Publications.PublicationID,
    Publications.PubName,
    GROUP_CONCAT(TRIM(Author2.AuthorID)ORDER BY Author2.AuthorID ASC)AS 'AuthorsAll'
FROM Authors AS Author1
LEFT JOIN Authors AS Author2
ON Author1.PublicationID = Author2.PublicationID
INNER JOIN Publications
ON Author1.PublicationID = Publications.PublicationID
WHERE Author1.AuthorID ="1" OR Author1.AuthorID ="2" OR Author1.AuthorID ="3" 
GROUP BY Author2.PublicationID

But it returns the following instead

 AuthorID | PublicationID | PublicationName | AllAuthors
     1    |       123     |        A        |    1,1,1,2,2,2,3,3,3
     1    |       456     |        B        |    1,1,3,3
     2    |       789     |        C        |     2

It does deliver the desired output when there is only one AuhorID in the where statement.
I have not been able to figure it out, does anyone know where i'm going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩发小公举 2024-09-03 05:45:43

要消除重复作者,请将:更改

ON Author1.PublicationID = Author2.PublicationID

为:

ON Author1.PublicationID = Author2.PublicationID AND
   Author1.AuthorID <> Author2.AuthorID

另外,将:更改

GROUP BY Author2.PublicationID

为:

GROUP BY Author1.AuthorID, Author2.PublicationID

To eliminate duplicate authors, change:

ON Author1.PublicationID = Author2.PublicationID

to:

ON Author1.PublicationID = Author2.PublicationID AND
   Author1.AuthorID <> Author2.AuthorID

Also, change:

GROUP BY Author2.PublicationID

to:

GROUP BY Author1.AuthorID, Author2.PublicationID
转瞬即逝 2024-09-03 05:45:43

我想我不确定为什么你首先需要 GROUP BY 。为什么不能像这样使用相关子查询:

Select   Author1.AuthorID
    ,  Publications.PublicationID
    , Publications.PubName
    , (
        Select GROUP_CONCAT(TRIM(Author2.AuthorID) ORDER BY Author2.AuthorID ASC) 
        From Authors As Author2
        Where Author2.PublicationID = Publications.PublicationID
        ) AS 'AuthorsAll'
FROM Authors AS Author1
    INNER JOIN Publications
        ON Author1.PublicationID = Publications.PublicationID
Where Author1.AuthorId In("1","2","3")

I suppose I'm not sure why you need the GROUP BY in the first place. Why couldn't you use a correlated subquery like so:

Select   Author1.AuthorID
    ,  Publications.PublicationID
    , Publications.PubName
    , (
        Select GROUP_CONCAT(TRIM(Author2.AuthorID) ORDER BY Author2.AuthorID ASC) 
        From Authors As Author2
        Where Author2.PublicationID = Publications.PublicationID
        ) AS 'AuthorsAll'
FROM Authors AS Author1
    INNER JOIN Publications
        ON Author1.PublicationID = Publications.PublicationID
Where Author1.AuthorId In("1","2","3")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文