现在有4个表,表结构都很简单,
id  name, 都是这样的结构,现在分别知道四个表中的name,想用一条语句把对应的4个id查出来就行,知道的说下,谢谢各位了SQL

解决方案 »

  1.   

    select id from tab1 where name='name1'
    union all
    select id from tab2 where name='name2'
    union all
    select id from tab3 where name='name3'
    union all
    select id from tab4 where name='name4'
    ;
      

  2.   

    对呀,这是union all的典型用法
      

  3.   

    楼上的都说了用union all,用并集就可以
    如果要去重的话,用union
    不去重用union all
      

  4.   

    union是可以都查出来,但是查出来好像是四列,我希望能查出来是四行,因为这个表结构一样,但是字段名称是不一样的,不知道怎么处理
      

  5.   

    搞定了,这样就可以了
    SELECT
      (select name1 from table1 where id1 = '1') name1,
      (select name2 from table2 where id2 = '2') name2,
      (select name3 from table3 where id3 = '3') name3
    from dual;
      

  6.   

    楼主是想四个ID 平行显示吧,就是每一行有四个表的ID ,然后显示所有?这样看看可以不?
    SELECT
       (select id from table1) name1,
       (select id from table2) name2,
       (select id from table3) name3,
       (select id from table4) name4
     from dual;
      

  7.   

    SELECT
       (select id from table1) name1,
       (select id from table2) name2,
       (select id from table3) name3,
       (select id from table4) name4
     from dual; 
      

  8.   

    以上都错误 你知道所有表中的name 举例 知道name 值是 a b c d
    应该这么写看好咯
    select t.* from 表1 t where t.name in ('a','b','c','d')
    union all
    select t2.* from 表2 t2 where t2.name in ('a','b','c','d')
    union all
    select t3.* from 表3 t3 where t3.name in ('a','b','c','d')
    union all
    select t4.* from 表4 t4 where t4.name in ('a','b','c','d')上面查出的做为一张表 再group by 一下看看多少个重复 需要取哪些数据
    select count(1) as 数量 ,t5.id , t5.name from 

    select t.* from 表1 t where t.name in ('a','b','c','d')
    union all
    select t2.* from 表2 t2 where t2.name in ('a','b','c','d')
    union all
    select t3.* from 表3 t3 where t3.name in ('a','b','c','d')
    union all
    select t4.* from 表4 t4 where t4.name in ('a','b','c','d')
    ) group by t5.id , t5.name