表T(ID ,FID,SXH)
数据(1,0,1
2,0,2
3,1,1
4,1,2
5,1,3
6,2,1
7,2,2)
fid是ID的父ID,SXH是本ID在父ID下的顺序号
我想通过SQL语句得到
ID,SXH和其父ID的sxh(没有父ID的记录可以忽略或返回空,或零)
即 id ,fid,sxh,f_sxh
( 1,0,1,0
2,0,2,0
3,1,1,1
4,1,2,1
5,1,3,1
6,2,1,2
7,2,2,2)
数据(1,0,1
2,0,2
3,1,1
4,1,2
5,1,3
6,2,1
7,2,2)
fid是ID的父ID,SXH是本ID在父ID下的顺序号
我想通过SQL语句得到
ID,SXH和其父ID的sxh(没有父ID的记录可以忽略或返回空,或零)
即 id ,fid,sxh,f_sxh
( 1,0,1,0
2,0,2,0
3,1,1,1
4,1,2,1
5,1,3,1
6,2,1,2
7,2,2,2)
解决方案 »
- 怎麼把兩條sql查詢結果串接在一起
- 求类似于PL/SQL中drop表的方法
- 有关两个数据表合并分组求各人得Money总和得SQL语句如何写?详情见下。
- 寻求亿级wap数据解决方案
- 这个语句有什么错??
- oracle initialization or shutdown in progress
- 菜鸟问题:网站要发布了,数据库怎么迁移/发布?
- oracle 9i 的存储过程形式参数的问题
- 在查询表时如何把同列的重复数据只列出一条,在线等待,急急急急
- 回滚段不能扩展,但空间够啊,为什么?
- 请教如何将朋友的DMP文件导入?
- delphi连接oracle时用system和sys以外的用户名和密码总是报错 怎么弄啊??
另外,f_sxh是什么含义呢?父亲在爷爷中的排位?select t1.id , t1.fid, t1.sxh, t2.sxh
from t t1, t t2
where t1.fid = t2.id这样行不?
比如记录4,1,2 其父ID 是1 ,1的顺序号为1结果应该为
4,1,2,1
我现在就想通过SQL语句得到这样的结果
比如记录4,1,2 其父ID 是1 ,1的顺序号为1结果应该为
4,1,2,1
我现在就想通过SQL语句得到这样的结果
with tmp as
(
select 1 id,0 fid,1 sxh from dual union all
select 2 id,0 fid,2 sxh from dual union all
select 3 id,1 fid,1 sxh from dual union all
select 4 id,1 fid,2 sxh from dual union all
select 5 id,1 fid,3 sxh from dual union all
select 6 id,2 fid,1 sxh from dual union all
select 7 id,2 fid,2 sxh from dual)
select mt.id, mt.fid, mt.sxh, nvl(st.sxh,0) f_sxh
from tmp mt, tmp st
where mt.fid = st.id(+)
order by mt.id; ID FID SXH F_SXH
---------- ---------- ---------- ----------
1 0 1 0
2 0 2 0
3 1 1 1
4 1 2 1
5 1 3 1
6 2 1 2
7 2 2 2