我目前有30多个表,每个表的结构都是完全一致的..
1.type table 1
name    str我需要从结构完全一致的type table 1中选出   name='char' 的结果..
这样的sql好像不行  select * from tableA  a,tableB  b  where  a.name='成' or b.name='成'2. type table 2 
基本也和上面一致..
不过时通过ID(自增主键) 来找select * from tableA  a where a.id=1 的结果集加上 select * from tableB b where b.id=.. (后面可能会有多个表)
请教一下怎么写这样的sql,最好事效率高一点的!

解决方案 »

  1.   

    1. 用UNION 
    select * from tableA  
    where  name='成'
    union all
    select * from tableB  
    where  name='成'或者先UNION再WHEREselect * from 
    (
    select * from tableA  
    union all
    select * from tableB  
    ) t
    where name='成'
    
      

  2.   

    2 也是基本相同select * from tableA 
    where id=1
    union all
    select * from tableB  
    where id=1
    union all
    ...
    union all
    select * from tableZ
    where id=1
      

  3.   


    1、
    SELECT * FROM (
    SELECT * FROM A
    UNION ALL
    SELECT * FROM B) A
    WHERE [name]='char'
    2、
    SELECT * FROM (
    SELECT * FROM A
    UNION ALL
    SELECT * FROM B) A
    WHERE [ID]=1
      

  4.   


    OR
    UNION 后生成临时表,再对临时表加WHERE 查找