开发者

Structuring a SQL statement to display by two levels depth not one upwards

开发者 https://www.devze.com 2023-02-28 23:15 出处:网络
SELECT parent.name FROM categories AS node, categories AS parent, categories AS parent1 WHERE node.left_node > parent.left_node AND node.right_node < parent.right_node AND parent.left_node >
SELECT parent.name 
FROM categories AS node,
categories AS parent,
categories AS parent1
WHERE node.left_node > parent.left_node AND node.right_node < parent.right_node AND parent.left_node > parent1.left_node AND < parent.right_node > parent1.right_node
AND node.name = '{$node_name}'
ORDER BY parent.right_node DESC

The query above 开发者_如何学编程will display all the parents to the root of the node called. I only want to get the next parent not all the way to the root, but only node above. For instance in the graphic below

http://img97.imageshack.us/img97/7139/fruittree.gif

I want to pull up FOOD when RED called Or Fruit when Cherry is called. For that I have created three level AS node, parent and parent1. Condition according to its values in the right and the left. I want to pull up those fields only WHERE node.left_node > parent.left_node AND node.right_node < parent.right_node AND parent.left_node > parent1.left_node AND < parent.right_node > parent1.right_node AND node.name = '{$node_name}'.

Don't know if the Condition it would be the right one in this case because it is throwing a fatal error with that query above.

any opinion?


In your original query you just need to do the following modifications:

SELECT parent1.name
FROM categories AS node,
categories AS parent,
categories AS parent1
WHERE node.left_node > parent.left_node AND node.right_node < parent.right_node
  AND parent.left_node > parent1.left_node AND parent.right_node < parent1.right_node
  AND node.name = '{$node_name}'
ORDER BY parent.right_node DESC
0

精彩评论

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