MySQL与Group By的一对多连接仅返回一个观察

时间:2021-10-24 15:54:17

I have a comment table and a tag table. For each comment, there could be multiple tags, or none. I want to join the two so I can get a list of tags for each comment.

我有一个评论表和一个标签表。对于每个评论,可能有多个标签,或者没有。我想加入这两个,所以我可以得到每个评论的标签列表。

CommentTable:

+---------+----------+---+
|CommentID|   Title  | ..|
+---------+----------+---+
|   1     |   animals|   |
|   2     |   plants |   |
+---------+----------+---+

TagTable:

+---------+----------+---+
|  TagID  |CommentID | ..|
+---------+----------+---+
|    5    |     1    |   |
|    6    |     1    |   |
|    7    |     3    |   |
+---------+----------+---+

So, a query should return the tags, (5,6) for a commentID == 1 and empty array for CommentID == 2

因此,查询应返回标记,(5,6)表示CommentID == 1,空数组表示CommentID == 2

This is what I have - it only selects the last ID and not multiples:

这就是我所拥有的 - 它只选择最后一个ID而不是倍数:

SELECT c.CommentID, c.Title,  t.TagID  FROM Comment as c
        LEFT OUTER JOIN Tag as t ON c.CommentID = t.CommentID
        GROUP BY t.TagID

3 个解决方案

#1


25  

You can use GROUP_CONCAT to turn data in multiple rows into a single delimited string:

您可以使用GROUP_CONCAT将多行中的数据转换为单个分隔的字符串:

SELECT    a.CommentID, 
          a.Title,
          GROUP_CONCAT(b.TagID ORDER BY b.TagID) AS tags
FROM      CommentTable a
LEFT JOIN TagTable b ON a.CommentID = b.CommentID
GROUP BY  a.CommentID,
          a.Title

In this case, if a comment does not have a corresponding tag, the field would just be NULL.

在这种情况下,如果注释没有相应的标记,则该字段将为NULL。


SQLFiddle Demo

#2


1  

try this:

SELECT c.CommentID, c.Title,  t.TagID  FROM Comment as c
        LEFT OUTER JOIN Tag as t ON c.CommentID = t.CommentID

edit1: If you want to return only one row per group as per the comment

edit1:如果你想根据评论每组只返回一行

SELECT c.CommentID, c.Title,MAX(t.TagID )
FROM Comment as c
left OUTER JOIN TagTable as t ON c.CommentID = t.CommentID
GROUP BY  c.CommentID, c.Title

#3


-1  

You don't need the group by for this situation:

在这种情况下,您不需要group by:

SELECT c.CommentID, c.Title,  t.TagID  FROM Comment as c
LEFT OUTER JOIN Tag as t ON c.CommentID = t.CommentID

#1


25  

You can use GROUP_CONCAT to turn data in multiple rows into a single delimited string:

您可以使用GROUP_CONCAT将多行中的数据转换为单个分隔的字符串:

SELECT    a.CommentID, 
          a.Title,
          GROUP_CONCAT(b.TagID ORDER BY b.TagID) AS tags
FROM      CommentTable a
LEFT JOIN TagTable b ON a.CommentID = b.CommentID
GROUP BY  a.CommentID,
          a.Title

In this case, if a comment does not have a corresponding tag, the field would just be NULL.

在这种情况下,如果注释没有相应的标记,则该字段将为NULL。


SQLFiddle Demo

#2


1  

try this:

SELECT c.CommentID, c.Title,  t.TagID  FROM Comment as c
        LEFT OUTER JOIN Tag as t ON c.CommentID = t.CommentID

edit1: If you want to return only one row per group as per the comment

edit1:如果你想根据评论每组只返回一行

SELECT c.CommentID, c.Title,MAX(t.TagID )
FROM Comment as c
left OUTER JOIN TagTable as t ON c.CommentID = t.CommentID
GROUP BY  c.CommentID, c.Title

#3


-1  

You don't need the group by for this situation:

在这种情况下,您不需要group by:

SELECT c.CommentID, c.Title,  t.TagID  FROM Comment as c
LEFT OUTER JOIN Tag as t ON c.CommentID = t.CommentID