關於SQL遞迴查詢問題,SQL問題 實現遞迴查詢

時間 2022-04-27 12:14:06

1樓:

select * from dbo.tbregioninfo where regionid not like '01%' and len(regionid)>2

我發現根本就沒必要用到你的tbregiontree表

看起來你的表沒有設計好。。。

2樓:匿名使用者

遞迴幹嘛,你regionid就是乙個絕對位址。like就好了。

sql問題 實現遞迴查詢

3樓:匿名使用者

--sql 2005 遞迴--> 測試資料:[tb]

if object_id('[tb]') is not null drop table [tb]

create table [tb]([id] int,[col1] varchar(8),[col2] int)

insert [tb]

select 1,'河北省',0 union all

select 2,'邢台市',1 union all

select 3,'石家莊市',1 union all

select 4,'張家口市',1 union all

select 5,'南宮',2 union all

select 6,'壩上',4 union all

select 7,'任縣',2 union all

select 8,'清河',2 union all

select 9,'河南省',0 union all

select 10,'新鄉市',9 union all

select 11,'aaa',10 union all

select 12,'bbb',10;with t as(

select * from [tb] where col1='河北省'

union all

select a.* from [tb] a ,t where a.col2=t.id)

select * from t

/*id col1 col2

----------- -------- -----------

1 河北省 0

2 邢台市 1

3 石家莊市 1

4 張家口市 1

6 壩上 4

5 南宮 2

7 任縣 2

8 清河 2(8 行受影響)*/

如何實現sql語句的遞迴查詢

4樓:海天盛

1.建立測試表,createtabletest_connect(idnumber,p_idnumber);

2.插入測試資料,

insertintotest_connectvalues(1,1);

insertintotest_connectvalues(2,1);

insertintotest_connectvalues(3,2);

insertintotest_connectvalues(4,3);

提交;3.查詢資料表的內容,選擇*fromtest_connect,4.執行遞迴查詢語句,將答案新增到nocycle元素中,就不會有[ora-01436:connectbyerrorintheuserdata]。執行結果如下:

select*

來自test_connectt

從id=4開始

由nocyclepriort連線。p_id=t.i.

5樓:糖糖寳寳

在sql server 2000 中你可以先一些自定義函式,或一些儲存過程,實現遞迴:

select level,typenamefrom producttype t

start with t.parentid=0connect by prior t.producttypeid= t.parentid;

6樓:匿名使用者

select * from table_name where id in (select id from table connect by prior id = pid start with id = 你想起始的id);

(select id from table connect by prior id = pid start with id = 你想起始的id);

這部分就可以遞迴查詢到所有以某乙個父id下的所有的包含父id在內的所有id

sql語句中怎麼實現遞迴查詢

7樓:

在sql server 2000 中你可以先一些自定義函式,或一些儲存過程,實現遞迴:

select level,typenamefrom producttype t

start with t.parentid=0connect by prior t.producttypeid= t.parentid;

問SQL的查詢問題,問乙個SQL的查詢問題

可以再新增許可權的時候 看以下 是否此使用者已經擁有此許可權 如果有了 就不要新增了 如果你想要刪除重複的記錄 可以參考這裡 如果你想在顯示的時候 不現實重複的 就可以用 group by 就向說的 使用者名稱設定為非空 許可權設定成主鍵 你所說的許可權列不重複是什麼意思?是在整個表中不重複 還是對...

關於SQL查詢的問題,乙個關於SQL查詢的問題。

select corpno,sum amt as sumamt from 表 group bycorpno,convert varchar 6 date,112 這個就是能實現你前兩列的顯示。乙個sql中不能按2種方法去彙總。所以total這一列得單獨彙總。然後再把資料聯合查詢一下。select c...

關於SQL語句查詢的問題

你還不會between的用法,後邊所跟的與前邊一樣的值型別就可以了,而不是邏輯表示式.比如 5 between 3 and 10 就會返回乙個true因為5是在此3和10之間的,需要注意的是這個是乙個封區間,也就是說3 betwwen 3 and 10和10 between 3 and 10都會返回...

sql查詢問題

a pc tran表有id吧?select count id from a pc tran where exchg amt 500 and cons no 要查詢的使用者編號 and exchg ym 3 查詢出特定使用者3月份繳費金額小於500的繳費記錄共多少次。select count id f...

SQL查詢語句求問

1 利用巢狀查詢 select from 學生表 where studentid in select studentid from 關係表 where teacherid teacherid 此處的 teacherid是變數,替換成老師id就可以了 2 利用內連線查詢,三張表交叉鏈結然後根據條件篩選...