日期:2014-05-18 浏览次数:20684 次
if object_id('[Table1]') is not null drop table [Table1]
go
create table [Table1] (postId int,sup_id int,realName nvarchar(4),postType int)
insert into [Table1]
select 1,12,'Tr',1 union all
select 2,13,'jk',1 union all
select 3,14,'pl',1
if object_id('[Table2]') is not null drop table [Table2]
go
create table [Table2] (postId int,sup_id int,realName nvarchar(4),postType int)
insert into [Table2]
select 1,12,'Tr',2 union all
select 2,13,'gk',2 union all
select 3,14,'hj',2
select * from [Table1]
select * from [Table2]
select postid,sup_id,realname,MAX(posttype)
from (
select postid,sup_id,realname,posttype from Table1
union
select postid,sup_id,realname,posttype from Table2 ) T
group by postid,sup_id,realname
/*
1 12 Tr 2
2 13 gk 2
2 13 jk 1
3 14 hj 2
3 14 pl 1*/