自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實(shí)現(xiàn)子孫樹查詢的經(jīng)典SQL語句

數(shù)據(jù)庫 SQL Server
使用SQL語句可以實(shí)現(xiàn)子孫樹查詢,下面就為您介紹該SQL語句的寫法,希望對(duì)您學(xué)習(xí)SQL語句的使用能夠有所幫助。

下面介紹的SQL語句非常經(jīng)典,該SQL語句實(shí)現(xiàn)子孫樹查詢,該SQL語句可以直接在查詢分析器中執(zhí)行,供您參考。

  1. --生成表  
  2. create table MENU(id int,mname char(50),parent int)  
  3.  
  4. --插入數(shù)據(jù)  
  5. insert into MENU   
  6. select 1,'新聞',Null union all  
  7. select 2,'房產(chǎn)',Null union all  
  8. select 3,'科技新聞',1 union all  
  9. select 4,'社會(huì)新聞',1 union all  
  10. select 5, 'IT新聞',3 union all  
  11. select 6, '航天新聞',3   
  12.  
  13. --實(shí)現(xiàn)查詢新聞子孫樹  
  14. Declare @s varchar(1000)   
  15. select @s=','+cast(id as varchar(20))+'' from MENU where id=1 
  16.  
  17. while  @@rowCount>0   
  18.  
  19. --charindex:返回字符串中指定表達(dá)式的起始位置  
  20.   select   @s=@s+','+cast(id as varchar) from MENU       
  21.             where charindex(','+cast(id as varchar)+',',@s+',')=0    
  22.    
  23.             and   charindex(','+cast(parent as varchar)+',',@s+',')>0   
  24.  
  25.  
  26. select * from MENU where charindex(','+cast(id as varchar)+',',@s+',')>0  
  27.  
  28. --刪除表  
  29.  
  30. drop table MENU  

 

 

 

【編輯推薦】

查詢表結(jié)構(gòu)的SQL語句

使用SQL語句查詢時(shí)間段

巧用SQL語句刪除重復(fù)記錄

批量執(zhí)行SQL語句的示例

教您使用SQL語句修改SQL主鍵

 

責(zé)任編輯:段燃 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-10-21 10:28:13

SQL Server查

2018-11-05 14:54:18

MySQLSQL語句數(shù)據(jù)庫

2010-10-27 17:00:32

oracle樹查詢

2010-10-21 12:16:11

SQL Server查

2010-09-26 15:23:24

SQL語句

2010-09-17 10:08:18

SQL中case wh

2010-09-28 14:33:13

SQL語句

2017-09-07 16:20:39

SQL查詢語句查詢優(yōu)化

2019-11-06 09:30:35

SQL查詢語句數(shù)據(jù)庫

2010-09-07 10:35:38

SQL語句

2010-09-25 16:53:39

SQL語句

2018-05-14 10:50:13

SQL查詢語句神經(jīng)網(wǎng)絡(luò)

2010-09-26 15:15:11

SQL語句

2010-09-24 19:23:51

SQL查詢時(shí)間段

2010-09-28 11:28:40

SQL字段屬性

2010-10-21 14:27:35

SQL Server時(shí)

2022-07-28 09:13:30

MySQL數(shù)據(jù)庫

2011-03-17 13:54:42

查詢參數(shù)SQL語句利用率

2010-09-07 14:36:24

SQL語句

2010-09-26 09:01:18

SQL強(qiáng)類型查詢
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)