比较有难度的SQL,100分求助!!!!

benzhuer 2012-02-19 08:40:39
样本数据
表1:有三列,编码、名称和上级编码
ID NAME PID
301 工资福利支出 #
30101 基本工资 301
30102 津贴补贴 301
30103 奖金 301
30104 社会保障缴费 301
3010401 医疗保险 30104
3010402 养老保险 30104
3010403 失业保险 30104

表2:有很多列,这里只列出与表1相关联的列
ID
30102
3010403

现在要写个SQL查询(存储过程也可以),需要查询出的结果为:
ID NAME PID
301 工资福利支出 #
30102 津贴补贴 301
30104 社会保障缴费 301
3010403 失业保险 30104
也就是说,结果要根据表2的ID从表1中取出相对应的数据,并且要有完整的级次,不知道我表述清楚没有
...全文
88 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
木小丰~ 2012-02-19
  • 打赏
  • 举报
回复
select *
from table1 t3, (select t1.id,t1.pid from table1 t1 , table2 t2
where t1.id = t2.id) t4
where t3.id = t4.pid or t3.id = t4.id;
木小丰~ 2012-02-19
  • 打赏
  • 举报
回复
select *
from table1 t3
where t3.id in (select t1.pid from table1 t1 , table2 t2
where t1.id = t2.id)
union
select t5.id,t5.pid from table1 t5 , table2 t6
where t5.id = t6.id;

能实现,感觉有点麻烦,不知道有没有简单点的方法。
木小丰~ 2012-02-19
  • 打赏
  • 举报
回复
select *
from table1 t3
where t3.id in (select t1.pid from table1 t1 , table2 t2
where t1.id = t2.id)
union
select t5.id,t5.pid from table1 t5 , table2 t6
where t5.id = t6.id;

能实现,感觉有点麻烦,不知道有没有简单点的方法。
benzhuer 2012-02-19
  • 打赏
  • 举报
回复
谢谢,结帖给分
rgcsm 2012-02-19
  • 打赏
  • 举报
回复
7楼正解
huan_lxyd 2012-02-19
  • 打赏
  • 举报
回复
select distinct ID,NAME,PID from 表1
connect by id = prior pid
start with id in (select id from 表2)
order by id
forgetsam 2012-02-19
  • 打赏
  • 举报
回复
select ID,NAME,PID from t1  
connect by id = prior pid
start with pid in (select id from t2)
forgetsam 2012-02-19
  • 打赏
  • 举报
回复
select ID NAME PID from t1
connect by id = prior pid
start with pid in (select id from t2)
IT葱头 2012-02-19
  • 打赏
  • 举报
回复
不知道我理解的对不对呀。。。。
select t1.*
from table1 t1,table2 t2
where t1.id = t2.id
order by t1.id,t1.pid

17,082

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧