易截截图软件、单文件、免安装、纯绿色、仅160KB

传智播客-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语言选择表中的第二条第三条第N条记录

如何用SQL语言选择表中的第二条第三条第N条记录
--ID为唯一性就行了
select top 1 * from table
where ID not in(select top 1 ID from table)--第2条
select top 1 * from table
where ID not in(select top 2 ID from table)--第3条
......

sql查找重复数据

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 ......

Firebird常用系统表Sql语句

--得到数据库的所有者名称
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 SERVER 数据类型详解


数据类型
类型
描              述
bit
整型
bit 数据类型是整型,其值只能是0、1或空值。这种数据类型用于存储只有两种可能值的数据,如Yes 或No、True 或Fa lse 、On 或Off
int
整型
int 数据类型可以存储从- 231(-2147483648)到231 (2147483 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号