树状结构的阶层数的sql
2010-12-11 17:05
344 查看
前提有一张组织表.
CREATE TABLE organization_
(
organizationid bigint NOT NULL,
companyid bigint,
parentorganizationid bigint,
leftorganizationid bigint,
rightorganizationid bigint,
"name" character varying(100),
type_ character varying(75),
recursable boolean,
regionid bigint,
countryid bigint,
statusid integer,
comments text,
CONSTRAINT organization__pkey PRIMARY KEY (organizationid)
)
树节点的阶层的sql如下:(阶层数从1开始)
SELECT Children.organizationid , COUNT(Parents.organizationid) AS level
FROM organization_ Parents, organization_ Children
WHERE Children.leftorganizationid BETWEEN Parents.leftorganizationid AND Parents.rightorganizationid
GROUP BY Children.organizationid;
结果例子:
13260,1
13290,1
10852,2
15300,3
CREATE TABLE organization_
(
organizationid bigint NOT NULL,
companyid bigint,
parentorganizationid bigint,
leftorganizationid bigint,
rightorganizationid bigint,
"name" character varying(100),
type_ character varying(75),
recursable boolean,
regionid bigint,
countryid bigint,
statusid integer,
comments text,
CONSTRAINT organization__pkey PRIMARY KEY (organizationid)
)
树节点的阶层的sql如下:(阶层数从1开始)
SELECT Children.organizationid , COUNT(Parents.organizationid) AS level
FROM organization_ Parents, organization_ Children
WHERE Children.leftorganizationid BETWEEN Parents.leftorganizationid AND Parents.rightorganizationid
GROUP BY Children.organizationid;
结果例子:
13260,1
13290,1
10852,2
15300,3
相关文章推荐
- 树状结构的罗列所有的节点的sql
- Oracle生成树状结构的SQL
- 实现了一个SQL查询整个树状结构数据的方法!找更好的!
- SQL 在一张表中表达树状结构
- [Oracle]生成树状结构的SQL
- SQL函数递归取树状结构
- T-SQL获取数据库中的Table表结构信息,Column结构信息
- --树状结构存储与展示
- Spark SQL 读取json 里面的数据 ,jason 是 结构的数据
- SQl 语句(常见) 新建,删除,修改表结构
- php实现树状结构无级分类
- sql清除表中所有数据,留下表结构
- 修改SQL-SERVER数据库表结构的SQL命令附sql命令行修改数据库
- MS SQL SERVER导出表结构到Excel
- pl /sql导入导出表结构,表数据,sql脚本
- MS SQL SERVER导出表结构到Excel
- SQL脚本修改表结构
- 利用 Sql 中查看表结构信息。
- Oracle树形结构的sql语句
- sql树结构,找到子节点和父节点,省市联动