表1: 
id  areaCode  field1  field2 
1    1000      2.036    5000.00 
2    1001      1.032    1000.00 
3    1002      0.325    3000.00 表2:(该表是递归形式的) 
id  areaCode  areaName  belongArea  areaGrade 
1  1000      中国      1000        0 
2  1001      湖南      1000        1 
3  1002      长沙      1001        2  select * from 表1,表2 where 表1.areaCode in(select areaCode from 表2 start with areaCode='地区变量' connect by prior  belongArea=areaCode); 假设“地区变量”=1002,表1中只有记录1和记录2,没有记录3。 
我现在的要求是如果在表1中找不到areaCode=1002的,就继续向上级地区查,找areaCode=1001的,如果1001能够匹配到记录,则停止向上查找。如果1001在表1中也不存在,就再继续向上级地区查,找areaCode=1000的。 问题:现在有什么办法能够让它在匹配到记录的时候就停止向上查呢?如果让它继续向上查的话,会产生多条记录.

解决方案 »

  1.   

    -- 看看是不是你想要的
    select areaCode, areaName
      from (select t.*, level lv
              from test t
             start with AREACODE = '1002'
            connect by prior BELONGAREA = AREACODE)
     where lv = 2;   -- 通过level,查出指定地区号的上一层.
      

  2.   

    今天周五,牛人都休息了....
    SQL> select * from t;
     
    ID AREACODE   FILD1
     -------- ---------
    1 1000         2
    2 1001         3
     
    SQL> select * from pt;
     
    ID AREACODE AREANAME        BELONGAREA
    -------- -------------------- ----------
    1 1000     中国                 1000
    1 1001     湖南                 1000
    1 1002     长沙                 1001
     
    SQL> select * from t where t.areacode in(select areacode from pt start with areacode ='1002' connect by prior belongarea=areacode);
     
    ID AREACODE    FILD1
     -------- ---------------------------------------
    2 1001           3
    1 1000           2SQL>select a.*,rownum from (select * from t 
                            where t.areacode in(select areacode from pt start with areacode ='1002' connect by prior belongarea=areacode)) a
    where rownum =1; 
    ID AREACODE    FILD1     ROWNUM
    ------------------------------------
    2 1001          3          1
      

  3.   

    -- 如果你的ORACLE DB是10G的话,把你表2中ID为1的BELONGAREA的1000改成NULL,试试看下面的语句:
    SQL> SELECT * FROM T1;        ID AREACODE     FIELD1     FIELD2
    ---------- ---------- -------- ----------
             1 1000          2.036    5000.00
             2 1001          1.032    1000.00
             3 1002          0.325    3000.00SQL> SELECT * FROM T2;        ID AREACODE   AREANAME   BELONGAREA  AREAGRADE
    ---------- ---------- ---------- ---------- ----------
             1 1000       中国                              0
             2 1001       湖南       1000                1
             3 1002       长沙       1001                2
             5 1003       浙江       1000                1
             6 1004       杭州       1003                2
             7 1005       滨江       1004                36 rows selected-- AREACODE = 1002
    SQL> SELECT *
      2    FROM (SELECT T1.*, LEV
      3            FROM (SELECT AREACODE, LEVEL LEV
      4                    FROM T2
      5                   START WITH AREACODE = '1002'
      6                  CONNECT BY NOCYCLE PRIOR BELONGAREA = AREACODE) TT,
      7                 T1
      8           WHERE TT.AREACODE = T1.AREACODE
      9           ORDER BY LEV) ZZ
     10   WHERE ROWNUM = 1;        ID AREACODE     FIELD1     FIELD2        LEV
    ---------- ---------- -------- ---------- ----------
             3 1002          0.325    3000.00          1-- AREACODE = 1003
    SQL> SELECT *
      2    FROM (SELECT T1.*, LEV
      3            FROM (SELECT AREACODE, LEVEL LEV
      4                    FROM T2
      5                   START WITH AREACODE = '1003'
      6                  CONNECT BY NOCYCLE PRIOR BELONGAREA = AREACODE) TT,
      7                 T1
      8           WHERE TT.AREACODE = T1.AREACODE
      9           ORDER BY LEV) ZZ
     10   WHERE ROWNUM = 1;        ID AREACODE     FIELD1     FIELD2        LEV
    ---------- ---------- -------- ---------- ----------
             1 1000          2.036    5000.00          2
      

  4.   

    我这两个SQL SERVRE的树查询不知道对你有没有用?/*
    标题:查询指定节点及其所有子节点的函数
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有子节点的函数
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @id , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.id , @level
        from tb a , @t_Level b
        where a.pid = b.id and b.level = @level - 1
      end
      return
    end
    go--调用函数查询001(广东省)及其所有子节点
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市
    003  001  深圳市
    004  002  天河区
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 10 行)
    */--调用函数查询002(广州市)及其所有子节点
    select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    002  001  广州市
    004  002  天河区(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有子节点
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    003  001  深圳市
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 7 行)
    */drop table tb
    drop function f_cid/*
    标题:查询指定节点及其所有父节点的函数
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有父节点的函数
    create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
    as
    begin
      insert into @t_level select @id
      select @id = pid from tb where id = @id and pid is not null
      while @@ROWCOUNT > 0
      begin
        insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
      end
      return
    end
    go--调用函数查询002(广州市)及其所有父节点
    select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有父节点
    select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    003  001  深圳市(所影响的行数为 2 行)
    */--调用函数查询008(西乡镇)及其所有父节点
    select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    003  001  深圳市
    007  003  宝安区
    008  007  西乡镇(所影响的行数为 4 行)
    */drop table tb
    drop function f_pid