手机
当前位置:查字典教程网 >编程开发 >mssql数据库 >SQL语句实现子孙树查询经典实例
SQL语句实现子孙树查询经典实例
摘要:下面介绍的SQL语句非常经典,该SQL语句实现子孙树查询,该SQL语句可以直接在查询分析器中执行,供您参考。--生成表createtable...

下面介绍的SQL语句非常经典,该SQL语句实现子孙树查询,该SQL语句可以直接在查询分析器中执行,供您参考。

--生成表 create table MENU(id int,mname char(50),parent int) --插入数据 insert into MENU select 1,'新闻',Null union all select 2,'房产',Null union all select 3,'科技新闻',1 union all select 4,'社会新闻',1 union all select 5, 'IT新闻',3 union all select 6, '航天新闻',3 --实现查询新闻子孙树 Declare @s varchar(1000) select @s=','+cast(id as varchar(20))+'' from MENU where id=1 while @@rowCount>0 --charindex:返回字符串中指定表达式的起始位置 select @s=@s+','+cast(id as varchar) from MENU where charindex(','+cast(id as varchar)+',',@s+',')=0 and charindex(','+cast(parent as varchar)+',',@s+',')>0 select * from MENU where charindex(','+cast(id as varchar)+',',@s+',')>0 --删除表 drop table MENU

【SQL语句实现子孙树查询经典实例】相关文章:

一个SQL语句获得某人参与的帖子及在该帖得分总和

SQLSERVER2005 中树形数据的递归查询

SQL Server 表交叉显示及实现方法

SQLServer2005 的查询独占模拟

SQL语句技巧:按月统计数据

SQL语句查询重复字段并按数量排序

初学者SQL语句

非常不错的SQL语句学习手册实例版第1/3页

SQL 时间类型的模糊查询

SQL查询连续号码段的巧妙解法

精品推荐
分类导航