我习惯在mysql中使用嵌套集并检索我通常会使用以下内容的单个路径:
SELECT node.treeID,
node.name,
node.lft,
node.rgt,
(COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM docsTree AS node,
docsTree AS parent,
docsTree AS sub_parent,
( SELECT node.name,
(COUNT(parent.name) - 1) AS depth
FROM docsTree AS node,
docsTree AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.treeID = :requestedNode
GROUP BY node.treeID,
node.name,
node.lft,
node.rgt
ORDER BY node.lft)AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.name = sub_tree.name
GROUP BY node.treeID,
node.name,
node.lft,
node.rgt HAVING depth = 1
ORDER BY node.lft
现在我需要在db2中执行相同操作,但上面的查询给出了以下错误:
错误:SELECT或HAVING子句中的列引用无效, 因为它不是分组列;或列引用 在GROUP BY子句中无效。
[SQL0122]专栏 SELECT列表中的DEPTH或表达式无效。 错误代码:-122
如何从db2返回嵌套的设置路径?
答案 0 :(得分:0)
在最后一组中,尝试使用
GROUP BY node.treeID,
node.name,
node.lft,
node.rgt HAVING (COUNT(parent.name) - (sub_tree.depth + 1)) = 1