我使用嵌套集模型在iPhone上的本地SQLite数据库中存储大型数据层次结构。我从他们的网站上读到MySQL tech article有关如何执行此操作的信息,但他们建议的(我需要的)查询之一似乎不适用于SQLite,我不知道如何绕过它
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM line_items AS node,
line_items AS parent,
line_items AS sub_parent,
(SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM line_items AS node,
line_items AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'Power Up'
GROUP BY node.name
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.name
HAVING depth <= 1
ORDER BY node.lft;
SQLite报告sub_tree.name
不是列,我认为这是因为它的子查询实现不完整。有没有人对如何解决这个限制有任何想法?
查询的目的是获取给定父节点的所有直接子节点。
答案 0 :(得分:3)
尝试在子查询中使用“node.name AS name”,即
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM line_items AS node,
line_items AS parent,
line_items AS sub_parent,
(SELECT node.name AS name, (COUNT(parent.name) - 1) AS depth
FROM line_items AS node,
line_items AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'Power Up'
GROUP BY node.name
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.name
HAVING depth <= 1
ORDER BY node.lft;
至少似乎摆脱了错误。