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
……想要查询出线路编号,起点序号,起点编号 ,终点序号,终点编号,请问该怎么写啊?

解决方案 »

  1.   

    select t1.XLID,t2.ZDID,t3.ZDID from table1 t1,table2 t2,table2 t3
    where t1.XH1 = t2.ZDSXH and t1.XH2 = t3.ZDSXH
      

  2.   

    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也不行,出来还是重复的,一条线路出来好多个记录,哪位老师指导下吧
      

  3.   

    少写了个条件
    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
      

  4.   

     and (a.xlid=b.xlid and a.xh2=c.zdsxh);
    原来我后面的一个条件写错了。。应该是a.xlid=c.xlid and a.xh2=c.zdsxh
    谢谢楼上的老师
      

  5.   

    又有问题了
    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);
    用上面的语句建了个视图,查询起来好慢啊,又不可以建索引,怎么做才能加快查询速度呢?