我正在尝试将Xoops帖子转换为Wordpress。作为其中的一部分,我想得到一个主题的评论计数。帖子和回复都在同一个“主题”上。如何计算它们并将其发布到新的列中?
数据库当前状态

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-1的计数)作为重放次数(comment count)。引导我使用MYSQL进行查询
我想把输出放在同一个表中。(评论计数)
DB预期输出
   | 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

关于mysql - 计算并插入Xoops数据库中的注释,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13173252/

10-10 06:32