读书人

三张表的格式都一模一样 如何合并成一

发布时间: 2012-02-08 19:52:21 作者: rapoo

三张表的格式都一模一样 怎么合并成一张表??
三张表的字段格式都一模一样 怎么合并成一张表



a表 b表 c表 d表
id name id name id name ?= id,name




[解决办法]
select * from (
select id,name from a表
union all
select id,name from b表
union all
select id,name from c表)d

[解决办法]

select id,name into D from a表
union
select id,name from b表
union
select id,name from c表

union和union all的区别是一个过滤重复记录一个不过滤重复记录
[解决办法]
--查3表的所有,用
select * from (
select id,name from a表
union all
select id,name from b表
union all
select id,name from c表)d

--查3表中有重的,用
select * from (
select id,name from a表
union
select id,name from b表
union
select id,name from c表)d

--union all 比 union 的效率高

[解决办法]
三张表的字段格式都一模一样 怎么合并成一张表



a表 b表 c表 d表
id name id name id name ?= id,name

select * into d from
(
select * from a
union all
select * from b
union all
select * from c
) t

读书人网 >SQL Server

热点推荐