0

このクエリをより複雑なクエリに実装するにはどうすればよいでしょうか?

SELECT count(`p`.`id`) as count 
FROM `wallPosts` `p` LEFT JOIN `wallPosts` `c` on `c`.`parentID` = `p`.`id` 
where `p`.`parentID` is not null group by `p`.`parentID`

戻り値:

+-------+
| count |
+-------+
|     3 |
|     2 |
+-------+

「複雑な」クエリを次のように単純化しました。

SELECT `p`.`id`, 
       `p`.`parentid`, 
       `p`.`commenterid`, 
       `p`.`userid`, 
       `p`.`post`, 
       `p`.`date`, 
       `p`.`tags`, 
       CASE 
         WHEN ( `p`.`userid` = `p`.`commenterid` 
                AND `p`.`parentid` IS NULL ) THEN 'true' 
         ELSE 'false' 
       end AS isMain 
FROM   `wallposts` `p` 
       LEFT JOIN `wallposts` `c` 
              ON `c`.`parentid` = `p`.`id` 
       LEFT JOIN `users` 
              ON `users`.`id` = `p`.`commenterid` 
GROUP  BY `p`.`id` 
HAVING `ismain` = 'true' 
ORDER  BY `p`.`date` DESC 
LIMIT  20 

戻り値:

+----+----------+-------------+--------+--------+---------------------+------+--------+
| id | parentID | commenterID | userID |  post  |        date         | tags | isMain |
+----+----------+-------------+--------+--------+---------------------+------+--------+
|  5 | NULL     |           1 |      1 | post#1 | 2013-07-26 13:29:02 | NULL | true   |
|  1 | NULL     |           1 |      1 | post#2 | 2013-07-26 13:28:23 | NULL | true   |
+----+----------+-------------+--------+--------+---------------------+------+--------+

これは私が欲しいものです:

+----+----------+-------------+--------+--------+---------------------+------+--------+-------+
| id | parentID | commenterID | userID |  post  |        date         | tags | isMain | count |
+----+----------+-------------+--------+--------+---------------------+------+--------+-------+
|  5 | NULL     |           1 |      1 | post#1 | 2013-07-26 13:29:02 | NULL | true   |     3 |
|  1 | NULL     |           1 |      1 | post#2 | 2013-07-26 13:28:23 | NULL | true   |     2 |
+----+----------+-------------+--------+--------+---------------------+------+--------+-------+

これは私の完全なテーブルです:

+----+----------+-------------+--------+---------+------+---------------------+
| id | parentID | commenterID | userID |  post   | tags |        date         |
+----+----------+-------------+--------+---------+------+---------------------+
|  1 | NULL     |           1 |      1 | post#1  | NULL | 2013-07-26 13:28:23 |
|  2 | 1        |           1 |      1 | reply#1 | NULL | 2013-07-26 13:28:28 |
|  3 | 1        |           1 |      1 | reply#2 | NULL | 2013-07-26 13:28:38 |
|  4 | 1        |           1 |      1 | reply#3 | NULL | 2013-07-26 13:28:54 |
|  5 | NULL     |           1 |      1 | post#2  | NULL | 2013-07-26 13:29:02 |
|  6 | 5        |           1 |      1 | reply#1 | NULL | 2013-07-26 13:29:05 |
|  7 | 5        |           1 |      1 | reply#2 | NULL | 2013-07-26 13:29:06 |
+----+----------+-------------+--------+---------+------+---------------------+

ご覧のとおり、各投稿の返信数を数えようとしています。

ご協力いただきありがとうございます!

4

2 に答える 2

1

同様のタイプの質問については、すでに回答済みです。http://sqlfiddle.com/#!2/9a6cc/9を参照してください。それはあなたのために働くかもしれません

SELECT `p`.`id`, 
       `p`.`parentid`, 
       `p`.`commenterid`, 
       `p`.`userid`, 
       `p`.`post`, 
       `p`.`date`, 
       `p`.`tags`, 
       CASE 
         WHEN ( `p`.`userid` = `p`.`commenterid` 
                AND `p`.`parentid` IS NULL ) THEN 'true' 
         ELSE 'false' 
       end AS isMain ,
        cnt.count1
FROM   `wallposts` `p` 
       LEFT JOIN `wallposts` `c` 
              ON `c`.`parentid` = `p`.`id` 
       LEFT JOIN `users` 
              ON `users`.`id` = `p`.`commenterid` 
       LEFT JOIN (SELECT count(`p`.`id`) as count1,parentid FROM `wallPosts` `p` where parentid is not null group by parentid) cnt ON 
                  cnt.parentid = p.id
GROUP  BY `p`.`id` 
HAVING `ismain` = 'true' 
ORDER  BY `p`.`date` DESC 
LIMIT  20 ;
于 2013-07-26T13:59:40.313 に答える