WEB开发网
开发学院数据库MSSQL Server 层次结构的数据库处理 阅读

层次结构的数据库处理

 2010-03-12 00:00:00 来源:WEB开发网   
核心提示: 可以通过给以前的查询添加 HAVING 子句来实现: SELECTnode.name,(COUNT(parent.name)-(sub_tree.depth+1))ASdepthFROMnested_categoryASnode,nested_categoryASparent,nested_ca

可以通过给以前的查询添加 HAVING 子句来实现:

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
HAVING depth <= 1
ORDER BY node.lft;
+---------------------------+-------+
| name                               | depth |
+---------------------------+-------+
| PORTABLE ELECTRONICS  |     0    |
| MP3 PLAYERS                   |     1    |
| CD PLAYERS                    |     1    |
| 2 WAY RADIOS                |     1    |
+---------------------------+-------+

如果不像显示父节点,将 HAVING depth <= 1 修改为 HAVING depth = 1.

上一页  5 6 7 8 9 10 

Tags:层次 结构 数据库

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接