MySQL连接3个表并计算另一个表
我正在做一个博客,里面有一篇文章,可以有标签和评论。我想尝试获得正确的SQL,以便在一次查询中获得它。 我有4张桌子:MySQL连接3个表并计算另一个表,mysql,sql,Mysql,Sql,我正在做一个博客,里面有一篇文章,可以有标签和评论。我想尝试获得正确的SQL,以便在一次查询中获得它。 我有4张桌子: article +------------+-----------------+------------+ | article_id | title | photo | +------------+-----------------+------------+ | 1 | This is a test | image1.jpg
article
+------------+-----------------+------------+
| article_id | title | photo |
+------------+-----------------+------------+
| 1 | This is a test | image1.jpg |
| 2 | Another Article | image2.jpg |
+------------+-----------------+------------+
article_tag
+------------+--------+
| article_id | tag_id |
+------------+--------+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
+------------+--------+
tags
+--------+------+
| tag_id | name |
+--------+------+
| 1 | tag1 |
| 2 | tag2 |
+--------+------+
comment
+------+---------+------------+
| name | message | article_id |
+------+---------+------------+
| 1 | hello | 1 |
| 2 | a | 2 |
+------+---------+------------+
我正试图得到这个:
+------------+----------------+------------+---------+----------+
| article_id | title | photo | tag_ids | comments |
+------------+----------------+------------+---------+----------+
| 1 | This is a test | image1.jpg | 1,2 | 1 |
+------------+----------------+------------+---------+----------+
这就是我到目前为止所做的:
SELECT a.article_id, a.title, a.photo, a.date, a.description_long, a.author, GROUP_CONCAT(tag_id) as `tag_ids`, COUNT(c.comment_id) as comments
FROM article as a
JOIN article_tag as at
ON a.article_id = at.article_id
LEFT JOIN comment as c
ON a.article_id = c.article_id
WHERE a.article_id = 1
但是我得到的评论是2而不是1?
非常感谢。
PS如果有人知道一种方法,我可以将标签ID从1,2更改为tag1,tag2,那将是令人惊讶的:-)标签和注释是独立的。因此,如果您有三个标记和两个注释,那么组合中会有6行 在MySQL中修复此查询的最简单方法是执行group by并在select中使用distinct:
SELECT a.article_id, a.title, a.photo, a.date, a.description_long, a.author,
GROUP_CONCAT(distinct tag_id) as `tag_ids`, COUNT(distinct c.comment_id) as comments
FROM article a JOIN
article_tag as at
ON a.article_id = at.article_id LEFT JOIN
comment c
ON a.article_id = c.article_id
WHERE a.article_id = 1
group by a.article_id
不过,我想说,修复查询的“正确”方法是修复连接。这将使用来自的中的子查询:
from . . .
(select article_id, group_concat(tag_id) as tags
from tags
group by article_id
) at
. . .
(select article_id, count(*) as numComments
from comments
group by article_id
) c
. . .
标记和注释是独立的。因此,如果您有三个标记和两个注释,那么组合中会有6行
在MySQL中修复此查询的最简单方法是执行group by并在select中使用distinct:
SELECT a.article_id, a.title, a.photo, a.date, a.description_long, a.author,
GROUP_CONCAT(distinct tag_id) as `tag_ids`, COUNT(distinct c.comment_id) as comments
FROM article a JOIN
article_tag as at
ON a.article_id = at.article_id LEFT JOIN
comment c
ON a.article_id = c.article_id
WHERE a.article_id = 1
group by a.article_id
不过,我想说,修复查询的“正确”方法是修复连接。这将使用来自的中的子查询:
from . . .
(select article_id, group_concat(tag_id) as tags
from tags
group by article_id
) at
. . .
(select article_id, count(*) as numComments
from comments
group by article_id
) c
. . .
这是因为有超过1个标记,因此生成了2行
Try COUNT(不同的c.comment\u id)。这将只计算不同的注释ID,因此重复的注释ID不会被计算两次
要获取标记名,请与标记表进行左连接。然后使用name列而不是tag id列对_CONCAT进行分组
以下是它应该是什么:
SELECT a.article_id, a.title, a.photo, a.date, a.description_long, a.author, GROUP_CONCAT(d.name) AS `tag_names`, COUNT( DISTINCT c.comment_id ) AS comments
FROM article as a
JOIN article_tag as at
ON a.article_id = at.article_id
LEFT JOIN comment as c
ON a.article_id = c.article_id
LEFT JOIN tags d ON at.tag_id = d.tag_id
WHERE a.article_id = 1
这是因为有超过1个标记,因此生成了2行
Try COUNT(不同的c.comment\u id)。这将只计算不同的注释ID,因此重复的注释ID不会被计算两次
要获取标记名,请与标记表进行左连接。然后使用name列而不是tag id列对_CONCAT进行分组
以下是它应该是什么:
SELECT a.article_id, a.title, a.photo, a.date, a.description_long, a.author, GROUP_CONCAT(d.name) AS `tag_names`, COUNT( DISTINCT c.comment_id ) AS comments
FROM article as a
JOIN article_tag as at
ON a.article_id = at.article_id
LEFT JOIN comment as c
ON a.article_id = c.article_id
LEFT JOIN tags d ON at.tag_id = d.tag_id
WHERE a.article_id = 1
在您的注释表中没有名为comment\u id
的列…count distinct,它返回2行,因为与article tag的连接很抱歉忘了在问题中包括这一行。但是我有一个,在你的评论表中没有一列名为comment\u id
,count distinct,它返回2行,因为与article tag的连接抱歉忘了在问题中包括这一行。但是我有一个顺便说一句,如果你想一次获得多篇文章的信息,用a.article_id替换WHERE子句为GROUP谢谢你的工作。我必须区分为GROUP_CONCAT(d.name)惊人:-)顺便说一句,如果您想一次获得多篇文章的此信息,请将WHERE子句替换为GROUP BY a.article_id谢谢您的工作。我必须分门别类地去组_CONCAT(d.name)惊人:-)