有多个表结构相同的表,期望得到的结果:多个表的查询跟一个表的查询类似,一个结果集
如下:
create table a(
id int primary key,
code nverchar2(6),
value nverchar2(50)
)
create table b(
id int primary key,
code nverchar2(6),
value nverchar2(50)
)
其中表a和表b中的id有肯能相同,但不同表的code不同
期望结果:查询出来的值好比是一个表查出来的效果

解决方案 »

  1.   


    select id,code,value from a
    union all select id,code,value from b;
      

  2.   

    to zhangandli
    不好意思,我弄错了,是我以前查的时候没有注意报错的信息,union all可以。以前是因为两张表结构不太一样。麻烦再问下,关于oracle中的临时表,如果用临时表处理上面的2张表,有什么好处和坏处?
      

  3.   

    在问个问题:
    怎么对查询结果字段作为条件进行查询问题补充如下:
    create table dict(
    id int primary key,
    codegroup nverchar2(6),
    code nverchar2(6),
    value nverchar2(100)
    )
    insert into dict values(1,'01','','a');
    insert into dict values(2,'01','011','b');
    insert into dict values(3,'01','012','c');
    insert into dict values(4,'01','013','d');
    create newTable(
    id int primary key ,
    code nverchar2(6)
    )
    insert into newTable values(1,'012');
    insert into newTable values(2,'013');
    查询newTable表,要求查询结果中包含dict表中codegroup=01  code=null的值------------下面是我实现的sql不过感觉有点怪,求高人给指点--------------------------------
    select id,code,
    (
    case when 1=1 then (select value from dict where id=(select id from dict where code is null and codegroup =(select max(codegroup) from dict where code = newTable.code)) end)
    )
     from newTable
      

  4.   

    seect * from dict,newTable where dict.id=newTable.id and dict.codegroup='01' and code=''
      

  5.   


    这个是啥意思?因为在newTable中只跟dict表中的code有对应关系,而现在需要查询newTable表和dict表中的codegroup的对应关系,  也就是在查询出netTable表中的code值的时候需要再次在dict表中进行一次查询得到codegroup的值---
    纠正一个自己的错误,数据类型nvarchar2写错了,写成nverchar2了
      

  6.   

    格式:SELECT * FROM a,b WHERE a.id = b.id AND a.id ='2';