现在有两个表:table1,table2,字段和记录如下
table1:
id a1   b1
1  001  111
2  002  222table2:
id a2   b2
1  003  333
2  004  444我想建一个视图vw,试图中结构如下:
id No   name
1  001  111
2  002  222
3  003  333
4  004  444用了如下语句
create or replace view vw as(
select table1.id,table1.a1,table1.b1
union all
select table2.id,table2.a2,table2.b2
from table1 , table2)
结果得到:
id a1   b1
1  001  111
2  002  222
1  003  333
2  004  444有两个问题没法解决:
1.视图的两个字段a1,b1改成No和name
2.视图的id字段应该是唯一的(就像1,2,3,4),应该如何加上这个标示?可以在建立视图的时候不select table1.id。另外视图中的a1字段的值都是唯一的,不会重复。能不能利用它来建立一个id?低级问题,达人求救,先行谢过!

解决方案 »

  1.   

    a1字段的值都是唯一的,不会重复
    ---------------
    那当然可以
    create or replace view vw as (
    select to_number(a1) id,table1.a1,table1.b1
    from table1
    union all
    select to_number(a2),table2.a2,table2.b2
    from table2)
      

  2.   

    create or replace view vw as
    select rownum id,a,b from 
    (select a1 a,b1 b from table1
    union all
    select a1 a,b1 b from table2 )如果需要排序
    create or replace view vw as
    select rownum id,a,b from 
    (select a1 a,b1 b from table1
    union all
    select a1 a,b1 b from table2 order by a)
      

  3.   

    创建一个SEQUENCE,序列是自增的,所以id就是唯一的,但不能用到视图中需要创建一个表.
    create sequence test
    minvalue 1
    maxvalue 1000000
    start with 1
    increment by 1
    cache 20;
    SELECT test.nextval,t3.a, t3.b
    FROM  (
    SELECT table1.a1 a,table1.b1 b
    union all
    SELECT table2.a2 a,table2.b2 b
    ) t3