如何在mysql中按左JOIN的结果/返回值分组?
我有3个表要加入 条目表如何在mysql中按左JOIN的结果/返回值分组?,mysql,sql,database,relational-database,Mysql,Sql,Database,Relational Database,我有3个表要加入 条目表 | entry_id | entry_title | |----------|----------------------------| | 1 | Hello World! | | 2 | Lorem Ipsum Dolor Sit Amet | | 3 | Foo Title Foo Title | 注释表 | comment_id | comment
| entry_id | entry_title |
|----------|----------------------------|
| 1 | Hello World! |
| 2 | Lorem Ipsum Dolor Sit Amet |
| 3 | Foo Title Foo Title |
注释表
| comment_id | comment_content | comment_entry_id |
|------------|-----------------------|------------------|
| 1 | lorem ipsum is great! | 1 |
| 2 | foo is great! | 1 |
| 3 | Hello World! | 2 |
| 4 | Hello Word! | 3 |
| threaded_comment_id | threaded_comment_content | threaded_comment_comment_id |
|---------------------|--------------------------|-----------------------------|
| 1 | i agree foo! | 2 |
| 2 | Yes Foo! | 2 |
| 3 | Lorem Ipsum is great! | 1 |
| 4 | Ah yes, Hello World! | 4 |
线程注释表
| comment_id | comment_content | comment_entry_id |
|------------|-----------------------|------------------|
| 1 | lorem ipsum is great! | 1 |
| 2 | foo is great! | 1 |
| 3 | Hello World! | 2 |
| 4 | Hello Word! | 3 |
| threaded_comment_id | threaded_comment_content | threaded_comment_comment_id |
|---------------------|--------------------------|-----------------------------|
| 1 | i agree foo! | 2 |
| 2 | Yes Foo! | 2 |
| 3 | Lorem Ipsum is great! | 1 |
| 4 | Ah yes, Hello World! | 4 |
以下是关于它的一些信息:
comment\u entry\u id
列是comment
表到entries
表的非识别外键threaded\u comment\u comment\u id
列是threaded\u comment
表到comment
表的非识别外键选择entries.entry\u title作为条目标题,
将(comment.comment\u id)计算为注释总数,
将(线程化注释。线程化注释\u id)计数为线程化注释的总数,
计数(comment.comment\u id)+计数(threaded\u comment.threaded\u comment\u id)作为\u注释和\u threaded\u注释的总数
从条目
左连接注释
ON entries.entry\u id=comment.comment\u entry\u id
左连接线程注释
在threaded\u comment.threaded\u comment\u comment\u id=comment.comment\u id上
按条目分组。条目\u id
这就是我得到的
| entry_title | total_of_comments | total_of_threaded_comments | total_of_comments_and_threaded_comments |
|----------------------------|-------------------|----------------------------|-----------------------------------------|
| Hello World | 2 | 1 | 3 |
| Lorem Ipsum Dolor Sit Amet | 2 | 2 | 4 |
| Foo Title Foo Title | 1 | 0 | 1 |
如您所见,在“Lorem Ipsum Dolor Sit Amet”行中,我得到了2条total_of_comments
,而我只得到了1条带有2条线程注释的注释。
我知道发生了这种情况,因为我没有按照注释.comment\u id
对进行分组,所以当注释中有多个线程注释时,它会重复相同的注释.comment\u id
所以我的问题是,在查询中已经有1个groupby
语句时,如何使用多个groupby
语句?或者我可以在左连接
查询中使用分组依据
语句吗
提前感谢。如评论所示,您可以仅在评论id列中应用COUNT DISTINCT
SELECT entries.entry_title,
COUNT(DISTINCT comment.comment_id) AS total_of_comments,
COUNT(threaded_comment.threaded_comment_id) AS total_of_threaded_comments,
COUNT(DISTINCT comment.comment_id) + COUNT(threaded_comment.threaded_comment_id) AS total_of_comments_and_threaded_comments
FROM entries
LEFT JOIN comment
ON entries.entry_id = comment.comment_entry_id
LEFT JOIN threaded_comment
ON threaded_comment.threaded_comment_comment_id = comment.comment_id
GROUP BY entries.entry_title
您可以遵循的另一种方法是计算子查询中的线程注释
SELECT entries.entry_title,
COUNT(comment.comment_id) AS total_of_comments,
CASE WHEN SUM(B.total_1) IS NULL THEN 0 ELSE SUM(B.total_1) END AS total_of_threaded_comments
FROM comment
LEFT JOIN (SELECT threaded_comment_comment_id, COUNT(threaded_comment_id) AS total_1
FROM threaded_comment
GROUP BY threaded_comment_comment_id)B
ON B.threaded_comment_comment_id = comment.comment_id
LEFT JOIN entries
ON entries.entry_id = comment.comment_entry_id
GROUP BY entries.entry_title
找到一个指示性的。也许你需要在简单的计数(不同的列)
中?这会将许多相同的列值折叠为1
。我同意@Akina的观点,我无法确切理解您想要的是什么,但可以肯定count distinct
就是答案。