由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 朋友圈遍历问题
相关主题
mySQL 问题 (转载)请教一个SQL Server的面试题
sql query helpAccess 里面两个 column不一样的table 能combine 到一起吗?
菜鸟问题,急高手请进
A rookie's query questionany group function reutrn null if there is null value?
问个sql/ ssis的问题 谢谢!SQL 2008 Group By Question
sql 请教How to split a column into several rows?
一个oracle performance 的问题。sql 题目求助
mysql mapping and insert questionperformance problem in Oracle Package
相关话题的讨论汇总
话题: col2话题: col1话题: where话题: cte话题: friend
进入Database版参与讨论
1 (共1页)
c*******r
发帖数: 3289
1
col1, col2两个column存放人名id, 要求给出任意一个人的id,找出所有和这个人有联
系的人,以及所有其他有联系的人。例如
col1 col2
---- ----
A B
A C
B J
M B
X J
C K
Q A
输入的人名是A, 需要返回
1. COL2 WHERE COL1 = A,输出B和C;
2. COL1 WHERE COL2 = A, 结果加入Q;
3. COL2 WHERE COL1 IN (B,C,Q), 加入结果;
4. COL1 WHERE COL2 IN (B,C,Q),加入结果;
...
如此类推直至找不到新的ID
这似乎是个递归问题,能写个store procedure吗
w****w
发帖数: 521
2
Something like:
with friend_cte as (
select col1 as col from friend where col2='a'
union
select col2 as col from friend where col1='a'
union
select col1 as col from friend join friend_cte
on friend.col2=friend_cte.col
union
select col2 as col from friend join friend_cte
on friend.col1=friend_cte.col
);
select col from friend_cte;
a*********y
发帖数: 63
3
This only works with MS SQL Server, also there is a limitation for number of
recursion for CTE though.
Better to use procedure with while loop.
1 (共1页)
进入Database版参与讨论
相关主题
performance problem in Oracle Package问个sql/ ssis的问题 谢谢!
谁能帮我看看这个Insert语句要怎么改一下?sql 请教
请教一个query 优化的问题(filter keyword)一个oracle performance 的问题。
请教一个SQL的问题mysql mapping and insert question
mySQL 问题 (转载)请教一个SQL Server的面试题
sql query helpAccess 里面两个 column不一样的table 能combine 到一起吗?
菜鸟问题,急高手请进
A rookie's query questionany group function reutrn null if there is null value?
相关话题的讨论汇总
话题: col2话题: col1话题: where话题: cte话题: friend