1

Xoops の投稿を Wordpress に変換しようとしています。その一環として、トピックのコメント数を取得したいと考えています。投稿と返信は同じ「topic_id」にあります。それらを数えて新しい列に投稿する方法は?

DBの現在のステータス

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) をリプレイ数 (コメント数) として取りたいと思います。MYSQL でクエリを実行する方法を教えてください

出力を同じテーブルに配置したい。(comment_count)

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
4

2 に答える 2

1
    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 
于 2012-11-01T08:18:18.630 に答える
0
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
于 2012-11-01T08:41:19.393 に答える