比如A表中有 field 这个字段,字段里面有以下数据  ID,name,age
B表中有ID,name,age字段 如何在A表中取出字段名在B表中查询相当于select id,name,age from b

解决方案 »

  1.   

     
    create table A
    (field varchar2(10));insert into a select 'id'from dual  union
    select 'name'from dual  union
    select 'age' from dualcreate table b
    (id varchar2(10),
    name varchar2(10),
    age varchar2(10));insert into b select '100','aa','21' from dual union
    select '101','bb','22' from dual union
    select '102','cc','23' from dualselect sum(decode(field,'id',b.id)) as id,max(decode(field,'name',b.name)) as name,
     max(decode(field,'age',b.age)) as age from a,b
     where id=b.id
     group by b.id
            ID NAME       AGE
    ---------- ---------- ---------
           100 aa         21
           101 bb         22
           102 cc         23
      

  2.   

     按照楼上这种说法,还是已经知道了b表中有哪些字段了,如果我要在a表中去状态位字段state为1的的field作为查询表b的的字段,那不是没有办法了。
      

  3.   

    可以使用pl/sql的动态sql拼sql语句
      

  4.   

    select   id,name,age   from   B
    where (id||','||name||','||age) in 
    (select  field  from A);