关于建立视图(两帖共150分)
之前有一个帖子http://topic.csdn.net/u/20080120/02/c4c486c4-9f37-4379-9fc3-1abef31404f3.html,
可能是问的不是很清楚,现整理如下,两帖一起结。
问题:
有数据表  A 结构如下(编号为主健)分类   来源   内容  编号
101     1             1
102     2             2
103     1             3
104     1             4有用户代码表B  结构如下(字段,代码为主健),以下是举例,真实表很长,如果因此表设计的不合理无法达到问题的要求,可以改动。字段   代码    解释
分类    101     分类1
分类    102     分类2
分类    103     分类3
分类    104     分类4
来源     1       本省
来源     2       外省请问,如何建立一个试图,达到如下的效果
分类        来源       内容  编号
分类1        本省             1
分类2        外省             2
分类3       本省              3
分类4        本省             4

解决方案 »

  1.   


    SQL> create table tab1(lb int,ly int,lr nvarchar2(30),bh int);Table createdExecuted in 0.015 secondsSQL> insert into tab1
      2  select 101,1,'',1 from dual
      3  union select 102,2,'',2 from dual
      4  union select 103,1,'',3 from dual
      5  union select 104,1,'',4 from dual;4 rows insertedExecuted in 0 secondsSQL> create table tab2(zd nvarchar2(20),code int,js nvarchar2(30));Table createdExecuted in 0.016 secondsSQL> insert into tab2
      2  select '分类',101,'分类1' from dual
      3  union select '分类',102,'分类2' from dual
      4  union select '分类',103,'分类3' from dual
      5  union select '分类',104,'分类4' from dual
      6  union select '来源',1,'本省' from dual
      7  union select '来源',2,'外省' from dual;6 rows insertedExecuted in 0.015 secondsSQL> create or replace view myview as
      2    select b.js "分类",c.js "来源",a.lr "内容",a.bh "编号" from tab1 a
      3    inner join tab2 b on a.lb = b.code
      4    inner join tab2 c on a.ly = c.code;View createdExecuted in 0.078 secondsSQL> /View createdExecuted in 0.125 secondsSQL> select * from myview;分类                                                         来源                                                         内容                                                                                            编号
    ------------------------------------------------------------ ------------------------------------------------------------ ------------------------------------------------------------ ---------------------------------------
    分类1                                                        本省                                                                                                                                                            1
    分类2                                                        外省                                                                                                                                                            2
    分类3                                                        本省                                                                                                                                                            3
    分类4                                                        本省                                                                                                                                                            4Executed in 0.016 secondsSQL> 
      

  2.   

    select 
    (select b.解释 from 用户代码表B b where a.分类=b.分类),
    (select c.解释 from 用户代码表B c where a.来源=c.来源),
    内容,
    编号
    from 数据表A a
    order by a.编号;
      

  3.   

    SQL> select * from test;     TYPES    SOURCES       V_ID
    ---------- ---------- ----------
           101          1          1
           102          2          2
           103          1          3
           104          1          4SQL> select * from test2;ZIDUAN                    CODES COMM
    -------------------- ---------- --------------------
    分类                        101 分类1
    分类                        102 分类2
    分类                        103 分类3
    分类                        104 分类4
    来源                          1 本省
    来源                          2 外省已选择6行。SQL> select (select comm from test2 where codes not in(1,2) and codes=a.types),
      2  (select comm from test2 where codes in(1,2) and codes=a.sources),
      3  v_id
      4  from test a
      5  /(SELECTCOMMFROMTEST2 (SELECTCOMMFROMTEST2       V_ID
    -------------------- -------------------- ----------
    分类1                本省                          1
    分类2                外省                          2
    分类3                本省                          3
    分类4                本省                          4