views:

57

answers:

1

I'm trying to introduce a join to this query:

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`

by adding

LEFT JOIN `exp_channel_titles`
ON (`n`.`entry_id`=`exp_channel_titles`.`entry_id`)

after the FROM statement...

But when I introduce it, it fails with "Unknown column 'n.entry_id' in 'on clause'"

Is it even possible to add a join to this query?

Can anybody help, thanks!

+1  A: 

Hello, I suppose your problem comes from the fact that you add the join clause at the end of the table list. Try

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`

You can only reference in an ON clause fields that belong to tables that are already in the JOIN stream.

I hope this will help you, Jerome Wagner

Jerome WAGNER
Thank you so much!!!!!
Iain Urquhart
can you accept the answer if it is ok for you ? thanks. Jerome
Jerome WAGNER
It wouldn't let me because you were so fast... have done now :)
Iain Urquhart
one thing I've just noticed, the count(*) is producing strange results now... not sure how to fix :(
Iain Urquhart
if you have several exp_channel_titles for 1 entry_id, the left_join will add these lines, maybe add a group by n.entry_id if this makes sense for your request
Jerome WAGNER