新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
實(shí)現(xiàn)子孫樹查詢的經(jīng)典SQL語句
下面介紹的SQL語句非常經(jīng)典,該SQL語句實(shí)現(xiàn)子孫樹查詢,該SQL語句可以直接在查詢分析器中執(zhí)行,供您參考。

成都創(chuàng)新互聯(lián)公司憑借在網(wǎng)站建設(shè)、網(wǎng)站推廣領(lǐng)域領(lǐng)先的技術(shù)能力和多年的行業(yè)經(jīng)驗(yàn),為客戶提供超值的營銷型網(wǎng)站建設(shè)服務(wù),我們始終認(rèn)為:好的營銷型網(wǎng)站就是好的業(yè)務(wù)員。我們已成功為企業(yè)單位、個(gè)人等客戶提供了成都做網(wǎng)站、成都網(wǎng)站建設(shè)服務(wù),以良好的商業(yè)信譽(yù),完善的服務(wù)及深厚的技術(shù)力量處于同行領(lǐng)先地位。
- --生成表
- create table MENU(id int,mname char(50),parent int)
- --插入數(shù)據(jù)
- insert into MENU
- select 1,'新聞',Null union all
- select 2,'房產(chǎn)',Null union all
- select 3,'科技新聞',1 union all
- select 4,'社會新聞',1 union all
- select 5, 'IT新聞',3 union all
- select 6, '航天新聞',3
- --實(shí)現(xiàn)查詢新聞子孫樹
- Declare @s varchar(1000)
- select @s=','+cast(id as varchar(20))+'' from MENU where id=1
- while @@rowCount>0
- --charindex:返回字符串中指定表達(dá)式的起始位置
- 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
【編輯推薦】
查詢表結(jié)構(gòu)的SQL語句
使用SQL語句查詢時(shí)間段
巧用SQL語句刪除重復(fù)記錄
批量執(zhí)行SQL語句的示例
教您使用SQL語句修改SQL主鍵
本文名稱:實(shí)現(xiàn)子孫樹查詢的經(jīng)典SQL語句
URL網(wǎng)址:http://www.dlmjj.cn/article/dhjsgcc.html


咨詢
建站咨詢
