Mysql 如何获得注释数量即使注释数量等于0
我有表格帖子和表格评论。评论已经提交了“post_id”,它引用了posts提交的“id”。我想得到每个帖子的评论数量。我使用这个查询:Mysql 如何获得注释数量即使注释数量等于0,mysql,sql,Mysql,Sql,我有表格帖子和表格评论。评论已经提交了“post_id”,它引用了posts提交的“id”。我想得到每个帖子的评论数量。我使用这个查询: SELECT COUNT(id) as count FROM comments WHERE post_id IN (1, 2, 3, 4, 5) GROUP BY id HAVING count >= 0 ORDER BY created_at DESC, id DESC select p.post_id, count(c.id) as tota
SELECT COUNT(id) as count
FROM comments
WHERE post_id IN (1, 2, 3, 4, 5)
GROUP BY id HAVING count >= 0 ORDER BY created_at DESC, id DESC
select p.post_id, count(c.id) as total_comments
from posts p LEFT JOIN comments c
on p.post_id = c.id
group by p.post_id
order by p.post_id desc;
但他只返回大于0的数量,但我需要得到也等于0的评论数量。试试这个:
SELECT post_id, COUNT(post_id)
FROM comment
WHERE post_id IN (1, 2, 3, 4, 5)
GROUP BY post_id
HAVING COUNT(post_id) >= 0
ORDER BY created_at DESC, post_id DESC
拥有
在这种情况下没有帮助。看起来您对某些帖子没有任何评论。所以,您不会从评论表中为此帖子选择任何记录。你需要这样的东西:
select p.id, count(c.id)
from posts p
left join comments c on p.id = c.post_id
where p.id in (1, 2, 3, 4, 5)
GROUP BY p.id
ORDER BY p.id DESC
因为它是空的 检查这个
SELECT COUNT(post_id) AS 'count'
FROM posts LEFT JOIN comments on post.id = comments.post_id
WHERE post.id IN (1, 2, 3, 4, 5)
GROUP BY post_id ORDER BY count DESC, created_at DESC
您需要从posts
表中获取帖子,有些帖子没有注释,因此post\u id
不存在于注释中使用此查询:
SELECT COUNT(id) as count
FROM comments
WHERE post_id IN (1, 2, 3, 4, 5)
GROUP BY id HAVING count >= 0 ORDER BY created_at DESC, id DESC
select p.post_id, count(c.id) as total_comments
from posts p LEFT JOIN comments c
on p.post_id = c.id
group by p.post_id
order by p.post_id desc;
说明:
select * from posts;
+----------+---------+
| postdesc | post_id |
+----------+---------+
| post-1 | 1 |
| post-2 | 2 |
| post-3 | 3 |
+----------+---------+
select * from comments;
+--------------+------+------------+
| comment_desc | id | created_at |
+--------------+------+------------+
| comment-1 | 1 | 2018-04-01 |
| comment-2 | 1 | 2018-04-02 |
| comment-3 | 2 | 2018-04-03 |
| comment-4 | 1 | 2018-04-04 |
+--------------+------+------------+
--query
+---------+----------------+
| post_id | total_comments |
+---------+----------------+
| 3 | 0 |
| 2 | 1 |
| 1 | 3 |
+---------+----------------+
更新:这是一个不使用联接语法的版本。给出与上面相同的结果
select a.post_id, a.total_comments
from
(
select post_id, 0 as total_comments
from posts
where post_id not in
(
select distinct id
from comments
)
union
select id, count(*)
from comments
group by id
) a
order by a.post_id desc;
请在您的问题中包含样本数据、表定义和预期输出将计数大于等于0的按id分组更改为计数大于等于0的按id分组检查我的答案,meI的工作是不需要连接表连接表是必要的,以便知道哪些post\u id
位于posts
表中,而comments
表中没有任何注释。例如,在上图中,post_id=3
在comments
表中没有任何注释。如果不连接它们,我们无法在结果中获得post\u id=3
的total\u comments=0
。您是否希望避免使用联接语法?更新了我的答案,以包含一个不使用联接语法的版本。我避免使用联接,因为我需要单独获取帖子和评论。因为Im需要缓存帖子,但不需要缓存评论的数量,所以请查看非连接语法是否更适合您的情况。我发布了一个带有备用查询的编辑。我不需要连接tables@Cannon431那个么,若在表中并没有记录,那个么您想如何为某个post_id选择记录呢?若您需要从查询中接收完整的id:count
列表,那个么您需要使用join
。换句话说,您可以初始化{'id':…,'count':0}
dict-In-code并更新query.Nope中的所有肯定结果。都一样。都一样。也许你按错误的列分组,请给我看你的表格评论:帖子:请检查上次编辑。使用左联接获取表注释中不存在的所有帖子