Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 删除用户评论,但保留它-计数_Mysql - Fatal编程技术网

Mysql 删除用户评论,但保留它-计数

Mysql 删除用户评论,但保留它-计数,mysql,Mysql,当用户删除帖子或评论时,我刚才在delete列中将其设置为1 问题是,我使用一个计数来显示用户在帖子上有多少评论 select c.nome, p.foto, c.user, p.user, count(DISTINCT comentarios.id) as comentarios_count from posts p join cadastro c on p.user=c.id left join comentarios on comentarios.foto = p.id where p

当用户删除帖子或评论时,我刚才在
delete
列中将其设置为1

问题是,我使用一个计数来显示用户在帖子上有多少评论

select c.nome, p.foto, c.user, p.user, count(DISTINCT comentarios.id) as comentarios_count from posts p 
join cadastro c on p.user=c.id 
left join comentarios on comentarios.foto = p.id
where p.delete='0' and comentarios.delete='0'
group by p.id
order by p.id desc limit ?
问题是comentarios。delete='0'将只选择至少有一条评论的帖子。我想选择所有帖子,但只统计
delete
设置为0的评论。
怎么了?

选择“全部”并减去删除的内容怎么样

select c.nome, p.foto, c.user, p.user, count(DISTINCT comentarios.id) -sum(comentarios.delete) as comentarios_count from posts p 
join cadastro c on p.user=c.id 
left join comentarios on comentarios.foto = p.id
where p.delete='0' 
group by p.id
order by p.id desc limit ?

选择“全部”并减去删除的内容怎么样

select c.nome, p.foto, c.user, p.user, count(DISTINCT comentarios.id) -sum(comentarios.delete) as comentarios_count from posts p 
join cadastro c on p.user=c.id 
left join comentarios on comentarios.foto = p.id
where p.delete='0' 
group by p.id
order by p.id desc limit ?

您可以使用条件和而不是计数,如下所示:

select c.nome, p.foto, c.user, p.user,
SUM(CASE comentarios.Deleted WHEN 0 THEN 1 ELSE 0 END)
as comentarios_count from posts p 
join cadastro c on p.user=c.id 
left join comentarios on comentarios.foto = p.id
where p.delete='0' 
group by p.id
order by p.id desc limit ?

它将计算未删除的评论,似乎比计算每篇文章的计数和总和要快

您可以使用条件总和而不是计数,如下所示:

select c.nome, p.foto, c.user, p.user,
SUM(CASE comentarios.Deleted WHEN 0 THEN 1 ELSE 0 END)
as comentarios_count from posts p 
join cadastro c on p.user=c.id 
left join comentarios on comentarios.foto = p.id
where p.delete='0' 
group by p.id
order by p.id desc limit ?


它将计算未删除的评论,似乎比计算每篇文章的计数和总和要快

您只选择了
delete='0'
的项目,所以它只计算它们的数量。@Clint我想计算一篇文章中删除为0的评论,并显示所有文章,即使是没有评论的文章。。。我的选择是只获取至少有一条评论的帖子。。。克林特:是的,他就是这么写的。。。。里克:实际上,只要选择全部并减去删除的总和。。。脏但应该有效您只选择了
delete='0'
中的项目,所以只计算它们。@Clint我想计算一篇delete为0的帖子的评论,并显示所有帖子,即使是没有评论的帖子。。。我的选择是只获取至少有一条评论的帖子。。。克林特:是的,他就是这么写的。。。。里克:实际上,只要选择全部并减去删除的总和。。。脏的,但应该工作啊,它的工作!像魔术一样-sum(comentarios.delete)仅获取设置为1的值?它只是求和。。因此,如果将一项设置为2或更多,则数字将不再正确。。正如我所说。它是脏的…另一种解决方案是使用count(DISTINCT if(comentarios.delete'1',comentarios.id,null)),这将使DISTINCT部分保持惊人!每一个都快吗?你认为我可以在我的网站spead有任何影响使用这个吗?在任何情况下,利用任何索引在这里是不可能的。。。如果你打算从数据库中提取大量数据,上述所有操作都不会很快完成。有时它可以帮助在内存中利用indexesoh准备临时表,这很有效!像魔术一样-sum(comentarios.delete)仅获取设置为1的值?它只是求和。。因此,如果将一项设置为2或更多,则数字将不再正确。。正如我所说。它是脏的…另一种解决方案是使用count(DISTINCT if(comentarios.delete'1',comentarios.id,null)),这将使DISTINCT部分保持惊人!每一个都快吗?你认为我可以在我的网站spead有任何影响使用这个吗?在任何情况下,利用任何索引在这里是不可能的。。。如果你打算从数据库中提取大量数据,上述所有操作都不会很快完成。有时,利用indexesit在内存中准备临时表也很有用!而且看起来真的更快!谢谢你,朋友!它也很好用!而且看起来真的更快!谢谢你,朋友!