用union来做查询 比如 t1 (id, name, c1) t2 (id, name, c2) select * from (select id, name, c1 col from t1 union all select id, name, c2 col from t2 ) where id = 1
用union all来查,union会去重复行。
union all 或者用动态SQL
用union all 来做查询 比如 t1 (id, name, c1) t2 (id, name, c2) t3 (id, name, c2) t4 (id, name, c2) t5 (id, name, c2) select * from (select id, name, c1 col from t1 union all select id, name, c2 col from t2 union all select id, name, c2 col from t3 union all select id, name, c2 col from t4 union all select id, name, c2 col from t5 ) where id = 1 其他的你自己试一下
其实可以认为的增加一个显示字段看是哪个表中的数据,这样也不会出现重复的情况了,如 select id, name, c1 col ,'t1' as Table from t1 union select id, name, c2 col ,'t2' as Table from t2
union all(id,name,age,birthday,phone)
select * from ( select id ,idcard ,phone from tb1 union all select id ,idcard ,phone from tb2 union all select id ,idcard ,phone from tb3 union all select id ,idcard ,phone from tb4 union all select id ,idcard ,phone from tb5 ) a where id =1
比如
t1 (id, name, c1)
t2 (id, name, c2)
select * from (select id, name, c1 col from t1
union all select id, name, c2 col from t2 ) where id = 1
或者用动态SQL
比如
t1 (id, name, c1)
t2 (id, name, c2)
t3 (id, name, c2)
t4 (id, name, c2)
t5 (id, name, c2)
select * from (select id, name, c1 col from t1
union all select id, name, c2 col from t2
union all select id, name, c2 col from t3
union all select id, name, c2 col from t4
union all select id, name, c2 col from t5 ) where id = 1
其他的你自己试一下
select id, name, c1 col ,'t1' as Table from t1
union
select id, name, c2 col ,'t2' as Table from t2
(
select id ,idcard ,phone from tb1
union all
select id ,idcard ,phone from tb2
union all
select id ,idcard ,phone from tb3
union all
select id ,idcard ,phone from tb4
union all
select id ,idcard ,phone from tb5
) a
where id =1