我正在使用嵌套集模型在iPhone上的本地SQLite数据库中存储较大的数据层次结构。我从他们的网站上阅读了MySQL技术文章,了解如何执行此操作,但是他们建议的查询(以及我需要的查询)之一似乎不适用于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不是列,我认为这是因为其子查询实现不完整。有人对如何解决此限制有任何想法吗?
sub_tree.name
查询的目的是获取给定父节点的所有直接子代。
尝试在子查询中使用“ node.name AS名称”,即
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;
至少似乎摆脱了错误。