请教个SQL语句

carefree_fish 2011-04-06 11:00:28
A表:线路编号,起点站序号,终点站序号(该表由B表生成而来,select xlid,min(to_number(zdsxh)) xh1,max(to_number(zdsxh)) xh2 from B group by xlid))
XLID XH1 XH2
10001 10 90
10002 10 80
10003 20 50
……

B表:线路编号,各站点序号及其对应的站点编号,其中站点顺序号最小的为起点站,最大的为终点站
XLID ZDID ZDSXH
10001 75810004 10
10001 74810002 20
10001 757F0000 30
10001 76800000 40
10001 777F0003 50
10001 7A7E0003 60
10001 79790004 70
10001 77760003 80
10001 77740004 90
10002 6E700002 10
10002 6F700000 20
10002 70700002 30
10002 72710003 40
10002 72710001 50
10002 74720003 60
10002 75730002 70
10002 76740002 80
10003 88720002 10
10003 8A720001 20
10003 8A730000 30
10003 8A730007 40
10003 8B740002 50
……

想要查询出线路编号,起点序号,起点编号 ,终点序号,终点编号,请问该怎么写啊?
...全文
105 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ojuju10 2011-04-07
  • 打赏
  • 举报
回复
在关联的条件中都建立组合索引
doer_ljy 2011-04-07
  • 打赏
  • 举报
回复
一种方法是对于试图所用字段所在的表添加索引
另一种方法是创建物化视图(实时性不好,我不推荐,哈哈)
carefree_fish 2011-04-06
  • 打赏
  • 举报
回复
and (a.xlid=b.xlid and a.xh2=c.zdsxh);
原来我后面的一个条件写错了。。应该是a.xlid=c.xlid and a.xh2=c.zdsxh
谢谢楼上的老师
doer_ljy 2011-04-06
  • 打赏
  • 举报
回复
少写了个条件
select t1.XLID,t2.ZDID,t3.ZDID from table1 t1,table2 t2,table2 t3
where t1.XH1 = t2.ZDSXH and
t1.XH2 = t3.ZDSXH and
t1.XLID = t2.XLID and
t1.XLID = t3.XLID
carefree_fish 2011-04-06
  • 打赏
  • 举报
回复
A表只有序号啊。。。我还想显示出对应的站点编号呀,关联了下,显示出来不对的。。。
好像关联问题,我一直都写不好
select a.xlid,a.xh1,b.zdid qdz from a ,b
where (a.xlid=b.xlid and a.xh1=b.ZDSXH);这样出来,起点站编号有了,对的

select a.xlid,a.xh1,b.zdid qdz,a.xh2, c.zdid zdz from a b ,(SELECT * FROM B ) c
where (a.xlid=b.xlid and a.xh1=b.ZDSXH) and
(a.xlid=b.xlid and a.xh2=c.zdsxh);
把and换成了OR也不行,出来还是重复的,一条线路出来好多个记录,哪位老师指导下吧
doer_ljy 2011-04-06
  • 打赏
  • 举报
回复
select t1.XLID,t2.ZDID,t3.ZDID from table1 t1,table2 t2,table2 t3
where t1.XH1 = t2.ZDSXH and t1.XH2 = t3.ZDSXH
horizonlyhw 2011-04-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 carefree_fish 的回复:]
想要查询出线路编号,起点序号,起点编号 ,终点序号,终点编号,请问该怎么写啊?
[/Quote]

这个不就是你的A表么?
carefree_fish 2011-04-06
  • 打赏
  • 举报
回复
又有问题了。。。
create or replace view zdb as
select a.xlid,a.xh1,b.zdid qdz,a.xh2, c.zdid zdz from a b ,(SELECT * FROM B ) c
where (a.xlid=b.xlid and a.xh1=b.ZDSXH) and
(a.xlid=C.xlid and a.xh2=c.zdsxh);
用上面的语句建了个视图,查询起来好慢啊,又不可以建索引,怎么做才能加快查询速度呢?

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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