|
create table tb1(id int,col1 varchar(10))
create table tb2(id int,col2 varchar(10))
insert tb1 select 1,'a'
union all select 2,'b'
union all select 3,'c'
union all select 4,'d'
insert tb2 select 1,'a'
union all select 1,'b'
union all select 2,'c'
union all select 2,'d'
union all select 3,'e'
union all select 3,'c'
union all select 3,'d'
union all select 1,'e'
union all select 4,'e'
select a1.id,col1,col2=(select max(col2) from tb2 where id=a1.id) from tb1 a1
drop table tb1,tb2
id col1 col2
----------- ---------- ----------
1 a e
2 b d
3 c e
4 d e
|