有3张表:
TableA(id,name) --记录地名 代码,名称。
如(01,北京);(02,上海);(03,广州);(04,沈阳)TableB(id,name) --记录国家代码,名称。
如(01,中国);(02,美国);(03,日本);(04,英国)TableC(id,type)  --记录中间数据(代码,类型)
如(01,a);(02,a) ; (03,b) 
其中a代表地区,b代表国家现在想 TableC 让实现如下效果:
01 , a , 北京
02 , a ,  上海
03 , b , 日本不知相关的sql语句怎么写,请各位大大不吝指教啊
不知道我有没有描述清楚。

解决方案 »

  1.   


    with tba as(
    select '01' id,'北京' name from dual union all
    select '02','上海' from dual union all
    select '03','广州' from dual union all
    select '04','沈阳' from dual),
    tbb as(
    select '01' id,'中国' name from dual union all
    select '02','美国' from dual union all
    select '03','日本' from dual union all
    select '04','英国' from dual),
    tbc as(
    select '01' id,'a' type from dual union all
    select '02','a' from dual union all
    select '03','b' from dual)
    --以上为提供数据,相当于你的表
    select tbc.id,tbc.type,tba.name
    from tbc,tba
    where tbc.id=tba.id
    and tbc.type='a'
    union all
    select tbc.id,tbc.type,tbb.name
    from tbc,tbb
    where tbc.id=tbb.id
    and tbc.type='b'ID T NAME
    -- - ----
    01 a 北京
    02 a 上海
    03 b 日本
      

  2.   

      想在一个select语句中实现,不用union关键词。。
      

  3.   


    select c.id,c.type,a.name from TableC c ,TableA a where 条件
    union all
    select c.id,c.type,b.name from TableC c ,TableB b where 条件不用union all的话select c.id,c.type,(a.name|b.name) from TableC c ,TableA a ,TableB B  where 条件
    这个语句怎么实现呢,我也想知道
      

  4.   

    --搞不懂为什么不能用union
    with tba as(
    select '01' id,'北京' name from dual union all
    select '02','上海' from dual union all
    select '03','广州' from dual union all
    select '04','沈阳' from dual),
    tbb as(
    select '01' id,'中国' name from dual union all
    select '02','美国' from dual union all
    select '03','日本' from dual union all
    select '04','英国' from dual),
    tbc as(
    select '01' id,'a' type from dual union all
    select '02','a' from dual union all
    select '03','b' from dual)
    select tbc.id,tbc.type,decode(tbc.type,'a',tba.name,tbb.name)
    from tbc,tba,tbb
    where (tbc.id=tba.id and tbc.id=tbb.id and tbc.type='a')
    or (tbc.id=tba.id and tbc.id=tbb.id and tbc.type='b')ID T DECO
    -- - ----
    01 a 北京
    02 a 上海
    03 b 日本
      

  5.   

    SELECT City_ID AS ID,
           'a'     AS TYPE,
           city_name AS NAME
    FROM   city
    UNION ALL
    SELECT country_id AS ID,
           'b'        AS TYPE,
           country_name  AS NAME
    FROM   country
           -- result:
    ID       TYPE NAME
    -------- ---- ----------
    01       a    北京
    02       a    上海
    03       a    广州
    01       b    中国
      

  6.   

    SQL> with tb1 as(
      2  select '01' id,'北京' name from dual union all
      3  select '02','上海' from dual union all
      4  select '03','广州' from dual union all
      5  select '04','沈阳' from dual),
      6  tb2 as(
      7  select '01' id,'中国' name from dual union all
      8  select '02','美国' from dual union all
      9  select '03','日本' from dual union all
     10  select '04','英国' from dual),
     11  tb3 as(
     12  select '01' id,'a' type from dual union all
     13  select '02','a' from dual union all
     14  select '03','b' from dual)
     15  select  b.id,b.type,decode(b.type,'a',a.name,'b',c.name) name
     16  from tb1 a,tb3 b,tb2 c
     17  where c.id=b.id and a.id=b.id
     18  /
     
    ID TYPE NAME
    -- ---- ----
    01 a    北京
    02 a    上海
    03 b    日本
      

  7.   

    用left join 
    select c.id,c.type,nvl(b.name,c.name)
    from tablec c
    left join tablea a on c.id=a.id and c.type='a'
    left join tableb b on c.id=b.id and c.type='b'
    ;
      

  8.   

    with tba as(
    select '01' id,'北京' name from dual union all
    select '02','上海' from dual union all
    select '03','广州' from dual union all
    select '04','沈阳' from dual),
    tbb as(
    select '01' id,'中国' name from dual union all
    select '02','美国' from dual union all
    select '03','日本' from dual union all
    select '04','英国' from dual),
    tbc as(
    select '01' id,'a' type from dual union all
    select '02','a' from dual union all
    select '03','b' from dual)
    select tbc.id,tbc.type,decode(tbc.type,'a',tba.name,'b',tbb.name,null)
    from tbc,tba,tbb
    where tbc.id=tba.id and tbc.id=tbb.id
      

  9.   


    select c.id,c.type,decode(c.type,'a',a.name,'b',b.name) name
    from tableA a,tableB b,tableC c
    where c.id=b.id and a.id=b.id