MySQL网站有关于在数据库中存储分层数据的excellent tutorial。我正在尝试编写返回节点的直接子节点的查询。我不想只是从MySQL网站复制/粘贴查询,因为我试图以数据库无关的方式处理这个问题。
这是我正在尝试Zend_Db_Select-ify
的查询SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM nested_category AS node,
nested_category AS parent,
nested_category AS sub_parent,
(
SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'PORTABLE ELECTRONICS'
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
ORDER BY node.lft;
答案 0 :(得分:1)
您可以尝试将查询拆分为两个Zend_Db_Select
语句 - 父查询和子查询。您可以使用Zend_Db_Select
对象作为from()
方法的参数,如下所示:
$mainQuery = $db->select();
$mainQuery->from('user');
$sub = $db->select();
$sub->from('company');
$mainQuery->from(array('subquery' => $sub));
你会得到那种查询:
SELECT `user`.*, `sub`.* FROM `user`
INNER JOIN (
SELECT `company`.* FROM `company`
) AS `sub`
如您所见,当您添加第二个INNER JOIN
时,它会自动添加from()
- 但我认为,您可以将查询重写为用户连接而不是语法中的多个连接。所以你应该使用joinInner()
方法,因为那时你可以指定连接条件作为它的第二个参数。
请注意,子查询与主查询类似,因此您可以构建主查询,将其克隆为子查询,并且由于Zend_Db_Select
可能删除不必要的部分(reset()
方法)并替换它们: / p>
$mainQuery = $db->select(); //and rest
$subQuery = clone $mainQuery;
$subQuery->reset(Zend_Db_Select::WHERE);
$subQuery->where(); // and add valid conditions for subquery
答案 1 :(得分:1)
您可以像这样直接粘贴查询
$result = $db->query("SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM nested_category AS node,
nested_category AS parent,
nested_category AS sub_parent,
(
SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'PORTABLE ELECTRONICS'
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
ORDER BY node.lf");
请务必使用quote()
在此查询中输入您自己的任何参数。
这有点像作弊,而不是我所采用的东西。