传智播客-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
相关文档:
--第一步
--在master库中建立一个备份数据库的存储过程.
USE master
GO
CREATE PROC p
@db_name sysname, --数据库名
@bk_path NVARCHAR(1024) --备份文件的路径
A ......
1.查找重复数据表的id以及重复数据的条数
select max(id) as nid,count(id) as 重复条数 from tableName
group by linkname Having Count(*) > 1
2.查找重复数据表的主键
select max(id) as nid from tableName
group by linkname Having Count(id) > 1
3.删除重复的数据
delete from table ......
从昨天开始就一直在网上查找数据库为SQL server 2000 的JSP开发环境的搭建,到现在为止终于能够正常联接并访问。以下是搭建的步骤:
1、从网上下载一个SQL server 2000 个人版的安装包,按照提示操作,当在身分验证时,选择混合模式,用户sa的密码可以为空,也可以不为空。
2、从网上下载一个SQ ......
最近很忙,有个项目马上要招标,一个项目等着工,若干琐碎的事进行中,有一段时间没更新些有营养的东西了
说个题外话先。
今天开机准备把昨天的东西debug一下,很习惯地右键项目的启动文件开始debug,机器突然蓝屏重启。开始以为又是内存在偷偷超频,检查了一下bios,发现没什么问题,也没怎么在意,隧重新开启vs2008继续 ......
SQL SERVER登录用户在服务器上的权限
一个登录用户到底在SQL SERVER实例上有什么样的权限,下面以SQL SERVER2005为例来细数一下。
一.
首先查看该登录用户属于哪个固定服务器角色。所有SQL SERVER的登录用户和角色都会在master.sys.server_principal视图上有一条记录。而记录登录用户属于什么服务器角色的视图是master. ......