源于一个想法,需要写一个SQL语句来实现,但没有成功。 
即:把表2中的一行数据,作为表1新增的一列显示出来。(行列的数目是对应的) 举例如下-------- 表1内容: 
ENFIELD    CHNAME 
  A          序号1 
  B          序号2 
  C          序号3 
  D          序号4 
  E          序号5 
...        ... 
  
------------ 表2内容: 
    A      B      C      D    E  ......  
  值1    值2    值3    值4    值5  ...... ------------ 要求SQL查询得到如下效果:把表2中的一行数据,作为表1新增的一列(VALUES)显示出来。 ENFIELD    CHNAME      VALUES 
  A          序号1         值1 
  B          序号2         值2 
  C          序号3         值3 
  D          序号4         值4 
  E          序号5         值5 
...        ...            ... ---------------------- 在此向各位请教。 多谢了! 

解决方案 »

  1.   


    如果单纯是一对一功能,可以这么实现啊:SELECT ENFIELD,
           CHNAME,
           DECODE(ENFIELD,
                  'A',
                  '值1',
                  'B',
                  '值2',
                  'C',
                  '值3',
                  'D',
                  '值4',
                  'E',
                  '值5')
      FROM TA;输出结果:
      A          序号1        值1 
      B          序号2        值2 
      C          序号3        值3 
      D          序号4        值4 
      E          序号5        值5 
      

  2.   


    如果第二个表有一行数据的话,也可以:
    SELECT ENFIELD,
           CHNAME,
           DECODE(ENFIELD, 'A', A, 'B', B, 'C', C, 'D', D, 'E', E)
      FROM TA, TB;输出结果:
      A          序号1        值1 
      B          序号2        值2 
      C          序号3        值3 
      D          序号4        值4 
      E          序号5        值5 
      

  3.   


    如果真实数据和测试一样都是“A”对于A列,“B"对于B列,............的话,可以使下面的方法生成decode那列,如下:DECLARE
     colNum INT  DEFAULT 5;
     str VARCHAR2(2000);
    BEGIN
     FOR i IN 1..colnum LOOP
         IF length(str)>0 THEN
         str:=str||','''||chr(64+i)||''','||chr(64+i);
         ELSE
          str:=''''||chr(64+i)||''','||chr(64+i);
         END IF;
     END LOOP;
     str:='decode(ENFIELD,'||str||')';
     dbms_output.put_line(str);
    END;然后利用上面生成的decode字符串,代替下面的decode字符串,就可以了。
    SELECT ENFIELD,
           CHNAME,
           DECODE(ENFIELD, 'A', A, 'B', B, 'C', C, 'D', D, 'E', E) vv
      FROM TA, TB;
      

  4.   

    try it ..
    SQL> select * from table1;ENFIELD CHNAME
    ------- ----------
    A       id1
    B       id2
    C       id3
    D       id4
    E       id5SQL> select * from table2;A          B          C          D          E
    ---------- ---------- ---------- ---------- ----------
    value1     value2     value3     value4     value5SQL> 
    SQL> select t1.ENFIELD, t1.CHNAME, tt2.cols_values
      2    from table1 t1,
      3         (select substr(',' || cols || ',',
      4                        instr(',' || cols || ',', ',', 1, 2 * rn - 1) + 1,
      5                        instr(',' || cols || ',', ',', 1, 2 * rn - 1 + 1) -
      6                        instr(',' || cols || ',', ',', 1, 2 * rn - 1) - 1) as new_cols,
      7                 substr(',' || cols || ',',
      8                        instr(',' || cols || ',', ',', 1, 2 * rn) + 1,
      9                        instr(',' || cols || ',', ',', 1, 2 * rn + 1) -
     10                        instr(',' || cols || ',', ',', 1, 2 * rn) - 1) as cols_values
     11            from (select rownum rn from all_objects where rownum <= 21) ao,
     12                 (select 'A' || ',' || A || ',' || 'B' || ',' || B || ',' || 'C' || ',' || C || ',' || 'D' || ',' || D || ',' || 'E' || ',' || E as cols
     13                    from table2) t2
     14           where instr(',' || cols, ',', 1, 2 * rn - 1) > 0) tt2
     15   where t1.ENFIELD = tt2.new_cols;ENFIELD CHNAME     COLS_VALUES
    ------- ---------- ------------------------------------------------------------------
    A       id1        value1
    B       id2        value2
    C       id3        value3
    D       id4        value4
    E       id5        value5SQL> 
      

  5.   

    mantisXF  的方法 不错!可以实现!已测试!
      

  6.   

    谢谢楼上各位!为了可以通用于SQLSERVER和Oracle,我在JAVA中拼写的SQL,没有使用存储过程和函数。最终拼写完成后的SQL大致的样子如下:Select ENFIELD,CHNAME,VALUE From t1 a INNER JOIN 
    (
    Select 'A' as ID,(select RTrim(A) from t2 ) as  VALUE FROM t2   --此处可以增加where条件
    union all
    Select 'B' as ID,(select RTrim(B) from t2 ) as  VALUE FROM t2
    union all
    Select 'C' as ID,(select RTrim(C) from t2 ) as  VALUE FROM t2
    --如果有,继续拼接
    ......
    ) b 
    on a.ENFIELD=b.ID 
    and ((ENFIELD='A'and VALUE='值1') or (ENFIELD='C'and VALUE= '值3')) --过滤条件。
    再次表示感谢。