传智播客-Tree in SQL(译文)(3)
This has some predictable results that we can use for building queries. The root is always of the form (left = 1, right = 2 * (SELECT COUNT(*) from TreeTable)); leaf nodes always have (left + 1 = right); the BETWEEN predicate defines the subtrees; and so on. Here are some common queries that you can use to build others:
我们可以利用一些可预见的结果构建查询(表达式)。根部总是以这种形式构成(left = 1, right = 2 * (SELECT COUNT(*) from TreeTable));叶子节点则总是(left + 1 = right);BETWEEN表达定义了子树;等等。这里有一些常用的查询,你可以用它们来组建更多其他(查询):
1. Find an employee and all his/her supervisors, no matter how deep the tree.
查找一个雇员及他/她的所有上级,无论这颗树结构有多深。
SELECT P2.*
from Personnel AS P1, Personnel AS P2
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
AND P1.emp = :myemployee;
2. Find the employee and all his/her subordinates. (This query has a nice symmetry with the first query.)
查找雇员及他/她所有下属。(这个查询与第一个查询形成良好的对称。)
SELECT P2.*
from Personnel AS P1, Personnel AS P2
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
AND P2.emp = :myemployee;
3. Add a GROUP BY and aggregate functions to these basic queries and you have hierarchical reports. For example, the total salaries that each employee controls:
给这些基本查询添加GROUP BY(关键字)和总计函数这样你就有分级记录了。例如,每个雇员能支配的薪酬总数:
SELECT P2.emp, SUM(S1.salary)
from Personnel AS P1, Personnel AS P2,
Salaries AS S1
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
AND P1.emp = S1.emp
GROUP BY P2.emp;
In the adjacency list method, this has to be done with a cursor.
在邻接列表模式里,必须依靠游标(cursor)才能这样做。
4. Find the level of each node, so you can print the tree as an indented listing.
查找每个节点的级别,这样你就能够以缩进列表形式打印这棵树了。
SELECT COUNT(P2.emp) AS indentation, P1.emp
from Personnel AS P1, Personnel AS P2
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
GROUP BY P1.emp
ORDER BY P1.lft;
5. The nested set mod
相关文档:
SQL Server 2005相对于SQL Server 2000来说,无论是性能还是功能都有一个相当大的提高,甚至可以用“革命”来形容这一次升级。SQL Server 2005使 SQL Server 跻身于企业级数据库行列。在数据高可用性方面,SQL Server 2005为用户提供了数据镜像、复制、故障转移群集、日志传送功能。本文向读者简单介结SQL Serve ......
--得到数据库的所有者名称
SELECT distinct RDB$OWNER_NAME AS DATABASE_OWNER
from RDB$RELATIONS
WHERE (RDB$SYSTEM_FLAG = 1);
--根据表名得到表的主键
SELECT RC.RDB$CONSTRAINT_NAME AS CONSTRAINT_NAME,
I.RDB$RELATION_NAME AS TABLE_NAME,
S.RDB$FIELD_NAME AS COLUMN_NAME
from RDB$RELATION_CONSTRAINTS ......
改善SQL语句
很多人不知道SQL语句在sql server中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解。比如:
select * from table1 where name=''zhangsan'' and tID > 10000
和执行:
select * from table1 where tID > 10000 and name=''zhangsan''
一些人不知道以上两条语句的执行效率是否一 ......
最近很忙,有个项目马上要招标,一个项目等着工,若干琐碎的事进行中,有一段时间没更新些有营养的东西了
说个题外话先。
今天开机准备把昨天的东西debug一下,很习惯地右键项目的启动文件开始debug,机器突然蓝屏重启。开始以为又是内存在偷偷超频,检查了一下bios,发现没什么问题,也没怎么在意,隧重新开启vs2008继续 ......