Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
db2,如何使用select查询从同一个表中获取父记录和子记录_Db2 - Fatal编程技术网

db2,如何使用select查询从同一个表中获取父记录和子记录

db2,如何使用select查询从同一个表中获取父记录和子记录,db2,Db2,你能给我一个主意吗,如何获得同一个表的父级和子级层次数据 我有一张这样的桌子: create table t7(wonum int, parent int, woid int); 我的数据如下所示: insert into t7(wonum ,parent, woid ) values(1024 , null, 101), (1025 , 1024, 102), (1028 , null, 186), (1029 , 1028, 157)

你能给我一个主意吗,如何获得同一个表的父级和子级层次数据

我有一张这样的桌子:

create table t7(wonum int, parent int, woid int);
我的数据如下所示:

insert into t7(wonum ,parent, woid )
values(1024 ,   null,      101),
(1025 ,   1024,      102),
(1028 ,   null,      186),
(1029 ,   1028,      157),
(1042,    1024,      123),
(1035 ,   1024,      103),
(1030 ,   null,      122),
(1031 ,   null,      108),
(1032 ,   null,      186),
(1034 ,   1028,      157),
(1035,    null,      123),
(1036 ,   1030,      103)
如何获得这样的结果表::

wonum   parent  woid
1024    null    101
1025    1024    102
1042    1024    123
1035    1024    103
1028    null    186
1029    1028    157
1034    1028    197
1030    null    122
1036    1030    143
1031    null    108
1032    null    156
1035    null    153

非常感谢您的帮助

我通过简单的Coalesce()和orderby子句得到了这个结果。
从t7顺序中选择wonum,Coalesce(父项,wonum),通过Coalesce(父项,wonum)选择wonum

研究递归SQL,公共表表达式(CTE)。返回您的(几乎)工作查询并共享SQL语句