Question

Trying to work with a nested set model.

Retrieving a Single Path, starting at 'Pakken', the parent of it is 'Heren', but since this is hierarchical data, 'Heren' could have an unlimited amount of parents to himself.

I'm stuck on the join, giving me this error

#1054 - Unknown column 'node.id' in 'on clause'

From this query where I try to join the product where his `products`.`category_id` must be equal to the selected id of the node.

SELECT *
FROM `categories` AS `node`,
     `categories` AS `parent`
INNER JOIN `products` ON `node`.`id` = `products`.`category_id`
WHERE `node`.`lft` BETWEEN `parent`.`lft` AND `parent`.`rght`
        AND `parent`.`id` = '1'
ORDER BY `node`.`lft`;

Code works fine without the INNER JOIN to it.

Was it helpful?

Solution

Solved it by changing a minor thing in the query, thanks to peterm!

The new query:

SELECT `products`.*
FROM `categories` AS `node`
JOIN `categories` AS `parent`
INNER JOIN `products` ON `node`.`id` = `products`.`category_id`
WHERE `node`.`lft` BETWEEN `parent`.`lft` AND `parent`.`rght`
        AND `parent`.`id` = '1'
ORDER BY `node`.`lft`;

Using a jon

OTHER TIPS

since you use the "comma": your first table is the table "node". your second table is the join of "parent" and "products". So the "ON" must contain columns from these two tables and not from "node". if you really want to use the "node.id=products.category_id", this must be placed in the WHERE clause.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top