由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - A question about recursive query
相关主题
Questions on SQL急问:导入XML文件到MySQL数据库
help! A bug about date type !Load .cvs to database
求教高手:odbc更新数据出错list duplicators in one table
SQL question(在线等,谢谢!!)请教关于exists在sql server中的用法
问一个小问题!Question about T-SQL
请教一下高手,包子答谢!请教set和select 的区别
SQL的一个愚蠢小问题请各位帮我看看这个最简单的Stored Procedure (转载)
How to find all duplicate record in SQL?question about import xml to sql table
相关话题的讨论汇总
话题: parent话题: sql话题: query话题: its话题: declare
进入Database版参与讨论
1 (共1页)
l******e
发帖数: 956
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
b**e
发帖数: 2
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 ?

n****f
发帖数: 905
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
1 (共1页)
进入Database版参与讨论
相关主题
question about import xml to sql table问一个小问题!
SQL请教...怎么写这样的distribution list请教一下高手,包子答谢!
请教一个有关SQL concat的问题SQL的一个愚蠢小问题
T-SQL 问题How to find all duplicate record in SQL?
Questions on SQL急问:导入XML文件到MySQL数据库
help! A bug about date type !Load .cvs to database
求教高手:odbc更新数据出错list duplicators in one table
SQL question(在线等,谢谢!!)请教关于exists在sql server中的用法
相关话题的讨论汇总
话题: parent话题: sql话题: query话题: its话题: declare