Redian新闻
>
A question about recursive query
avatar
A question about recursive query# Database - 数据库
l*e
1
I have a table like
id parent_id lastname firstname
1 0 A 1
2 0 B 1
3 1 A 2
4 3 A 3
....
So you can see id 1 is parent of id 3, id 3 is parent of id 4
How to right a single SQL query, when you give id = 1, it will return its own
record along with id 3 and id 4 records as its child and grandchild ?
Also, how to write a SQL query, when you gice id = 4, it will return its own
re
avatar
b*e
2
For your first question, and for the second onw it's pretty much same.
Sorry it's not simple. We have to wait for Yukon to write simple recursive
queries
(For microsoft sql server)
Create Table #tmpTABLE (ID int, parent_id int, lastName varchar(50), firstName
varchar(50) )
declare xTmp scroll cursor for SELECT * from tablename
open xTmp
declare @myid int
declare @pid int
declare @lName varchar(50)
declare @fName varchar(50)
declare @found bit
declare @tmpID int
declare @tmpPID int
declare @num i

【在 l******e 的大作中提到】
: I have a table like
: id parent_id lastname firstname
: 1 0 A 1
: 2 0 B 1
: 3 1 A 2
: 4 3 A 3
: ....
: So you can see id 1 is parent of id 3, id 3 is parent of id 4
: How to right a single SQL query, when you give id = 1, it will return its own
: record along with id 3 and id 4 records as its child and grandchild ?

avatar
n*f
3
Q: How to right a single SQL query, when you give id = 1, it will return its
own record along with id 3 and id 4 records as its child and grandchild ?
SELECT *
FROM Your_Table_Name
CONNECT BY PRIOR ID = parent_id
START WITH ID = 1
Q. Also, how to write a SQL query, when you gice id = 4, it will return its
own
record along with id 3 and id 1 records as its parent and grandparent ?
SELECT *
FROM Your_Table_Name
CONNECT BY PRIOR parent_id = id
START WITH ID = 4
这是典型的 recursive q
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。