添加 LEFT JOIN 后损坏的计数(*)
自从将 LEFT JOIN 添加到下面的查询后,count(*) 一直返回一些奇怪的值,它似乎已将查询中返回的总行数添加到“级别”:
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`
我完全被难住了......谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想知道计数,只需运行不带
group by
的查询即可。您将看到计数中的行来自何处。在您的情况下,您可以通过将
count(*)
更改为count unique(n.entry_id)
来修复此问题。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(*)
tocount distinct(n.entry_id)
.