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 计算并插入Xoops数据库中的注释_Mysql_Sql_Count - Fatal编程技术网

Mysql 计算并插入Xoops数据库中的注释

Mysql 计算并插入Xoops数据库中的注释,mysql,sql,count,Mysql,Sql,Count,我正在尝试将Xoops帖子转换为Wordpress。作为其中的一部分,我想获得一个主题的评论计数。帖子和回复在同一个主题id上。如何计算它们并将其发布到新列中 数据库当前状态 从这里,我想将topic_id-1的计数作为重播评论计数。指导我在MYSQL中进行查询 我想把输出放在同一个表中。评论与计数 DB预期输出 这将为3 | hello friends的评论计数=1,因为它没有评论重播,所以应该为0。如果有评论重播,怎么可能为零呢?因为没有对此评论的重播评论,所以应该为零。查看OP示例数据。1

我正在尝试将Xoops帖子转换为Wordpress。作为其中的一部分,我想获得一个主题的评论计数。帖子和回复在同一个主题id上。如何计算它们并将其发布到新列中

数据库当前状态

从这里,我想将topic_id-1的计数作为重播评论计数。指导我在MYSQL中进行查询

我想把输出放在同一个表中。评论与计数

DB预期输出


这将为3 | hello friends的评论计数=1,因为它没有评论重播,所以应该为0。如果有评论重播,怎么可能为零呢?因为没有对此评论的重播评论,所以应该为零。查看OP示例数据。1 | welcome的commnet|u count=1,因为它有1个replay,但3 | hello friends没有任何replay。可以使用null代替“0”。我想在同一时间更新table@MahmoudGamal这会有什么区别吗..和指出我的错误的10倍。actull这不是一个糟糕的db设计实践吗???这会给3个注释计数=1 |你好朋友,它应该是0,因为它没有注释回放。如果有注释回放,它怎么可能是零呢?因为没有回放,它应该是零对这一评论的评论。查看OP示例数据。1 | welcome的commnet|u count=1,因为它有1个replay,但3 | hello friends没有任何replay。可以使用null代替“0”。我想在同一时间更新table@MahmoudGamal这会有什么不同吗..和10倍,因为我指出了我的错误。这不是一个糟糕的数据库设计实践吗???我用表名更改了。选择sh_bb_posts.topic_id,topic,wp_posts\u temp.comment_count从sh_bb_posts加入选择sh_bb_posts.topic_id是否正确,注释\u count=COUNT1-1来自sh_bb_posts组,由sh_bb_posts.topic_id作为wp_posts\u temp在sh_bb_posts.topic_id=wp_posts\u temp.topic_IDLooks对我来说是正确的:如果你希望它100%明确,请使用下面的内容。我从子查询中删除了表前缀,并将表名添加到SELECT的主题部分:SELECT sh_bb_posts.topic_id、sh_bb_posts.topic、wp_posts_temp.comment_count from sh_bb_posts JOIN SELECT topic_id、,注释计数=COUNT1-1由sh_bb_posts.topic_id作为sh_bbu posts.topic_id=wp_posts.topic_id=wp_posts.temp.topic_id随表名更改。选择sh_bb_posts.topic_id,topic,wp_posts\u temp.comment_count从sh_bb_posts加入选择sh_bb_posts.topic_id是否正确,注释\u count=COUNT1-1来自sh_bb_posts组,由sh_bb_posts.topic_id作为wp_posts\u temp在sh_bb_posts.topic_id=wp_posts\u temp.topic_IDLooks对我来说是正确的:如果你希望它100%明确,请使用下面的内容。我从子查询中删除了表前缀,并将表名添加到SELECT的主题部分:SELECT sh_bb_posts.topic_id、sh_bb_posts.topic、wp_posts_temp.comment_count from sh_bb_posts JOIN SELECT topic_id、,评论数量=COUNT1-1由sh_bb_posts.topic_id作为sh_bb_posts.topic_id=wp_posts.topic_id作为wp_posts_temp在sh_bb_posts.topic_id=wp_posts_temp
topic_id | subject             |comment_count|
+________+_____________________+_____________+
1    | welcome             | 
1    | Re: welcome         |
2    | hello world         |
2    | Re: hello world     |
2    | Re: hello world     |
3    | hello friends       |
   | topic_id | subject             |comment_count|
   +________+_____________________+_____________+
   | 1   | welcome             | 1 
   | 1   | Re: welcome         | 1
   | 2   | hello world         | 2
   | 2   | Re: hello world     | 2
   | 2   | Re: hello world     | 2
   | 3   | hello friends       | 0
    select *,t2.comment_count  from table t1 
     join
     (
       select count(*),CONCAT('Re', ' ', subject) 
        as replay,topic_id as comment_count 
        from table 
        where suject=replay group by topic_id
     ) as t2 on t1.topic_id=t2.topic_id 
SELECT 
    xoops.topic_id, topic, xoops2.commentCount
FROM 
    xoops JOIN 
(
    SELECT 
        xoops.topic_id,
        commentCount = COUNT(1) - 1
    FROM 
        xoops   
    GROUP BY 
        xoops.topic_id
        ) AS xoops2 ON xoops.topic_ID = xoops2.topic_ID