还有其他方法么?
如果你是要两个表合并后用where条件的话,那就不如来个子查询了?
select c.* from (select id,name,time from a union select id,name,time from b) as c where ……
不用union的话,只能将两个结果集插入一个临时表,再查询了。
两种方法效率都比较低,不适合大量数据
if exists(SELECT NAME FROM tempdb.dbo.sysobjects where NAME like '%#tmp%' and Type = 'U')
drop table #tmp
create table #tmp
(
id int ,
name varchar(255),
time datetime
)
insert into #tmp select id,name,time from 表1
insert into #tmp select id,name,time from 表2
select * from #tmp
union 多简单啊。
select id=isnull(t1.id,t2.id),name=isnull(t1.name,t2.name),
time=isnull(t1.time,t2.time) from 表1 t1 full join 表2 t2 on 1=2