开发者

Broken count(*) after adding LEFT JOIN

Since adding the LEFT JOIN to the query below, the count(*) has been returning some strange values, it seems to have added the total rows returned in the query to the 'level':

SELECT `n`.*, exp_channel_titles.*, round((`n`.`rgt` - `n`.`lft` - 1) / 2, 0) AS childs,  
count(*) - 1 + (`n`.`lft` > 1) + 1 AS level,  
((min(`p`.`rgt`) - `n`.`rgt` - (`n`.`lft` > 1)) / 2) > 0 AS lower,  
(((`n`.`lft` - max(`p`.`lft`) > 1))) AS upper  
FROM `exp_node_tree_6` `n` 
    LEFT JOIN `exp_channel_titles` 
    ON (`n`.`entry_id`=`exp_channel_titles`.`entry_id`), 
    `exp_node_tree_6` `p`,  
    `exp_node_tree_6`  
WHERE `n`.`lft`  
BETWEEN `p`.`lft`  
AND `p`.`rgt`  
AND ( `p`.`node_id` != `n`.`node_id` OR `n`.`lft` = 1 ) 
GROUP BY `n`.`node_id`  
ORDER BY `n`.`lft`

I'开发者_运维知识库m totally stumped... Thank you!


If you wonder about a count, just run the query without a group by. You'll see where the rows in the count come from.

In your case, you could probably fix it by changing count(*) to count distinct(n.entry_id).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜