如何将此查询实施到更复杂的查询中?
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 |
+----+----------+-------------+--------+---------+------+---------------------+
正如您所看到的,我正在尝试计算每个帖子的回复数量。
感谢您的帮助!
答案 0 :(得分: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 ;
答案 1 :(得分:0)
搞定了:
SELECT
`p`.`id`,
`p`.`parentID`,
`p`.`commenterID`,
`p`.`userID`,
`p`.`post`,
`p`.`date`,
`p`.`tags`,
`commentCount`.`count`,
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`
LEFT JOIN (
SELECT `parentID`, COUNT(*) AS count
FROM `wallPosts` where `parentID` is not null
GROUP BY `parentID`
) AS `commentCount`
ON `p`.`id` = `commentCount`.`parentID`
GROUP BY `p`.`id`
having `isMain` = 'true'
ORDER BY `p`.`date` DESC
limit 20