为此查询引入连接,可能吗?

时间:2010-04-04 13:12:04

标签: sql join nested-sets

我正在尝试为此查询引入连接:

SELECT `n`.*, 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`, `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`

添加

LEFT JOIN `exp_channel_titles`
ON (`n`.`entry_id`=`exp_channel_titles`.`entry_id`)
FROM语句后的

......

但是当我介绍它时,它会因'on clause'中的“未知列'n.entry_id'而失败”

甚至可以为此查询添加联接吗?

任何人都可以帮忙,谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您的问题来自于在表列表的末尾添加join子句的事实。尝试

SELECT `n`.*, 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`

您只能在 ON 子句中引用属于JOIN流中已有的表的字段。

我希望这会对你有所帮助, 杰罗姆瓦格纳