34,590
社区成员
发帖
与我相关
我的任务
分享
if object_id('tb_A') is not null
drop table tb_A
go
create table tb_A(account varchar(10),name varchar(10))
go
insert into tb_A
select 'ap\test1','测试1' union all
select 'ap\test2','测试2' union all
select 'ap\test3','测试3'
go
if object_id('tb_B') is not null
drop table tb_B
go
create table tb_B(account varchar(10),bk_id int,date varchar(10))
go
insert into tb_B
select 'ap\test1',14,'2008-1-1' union all
select 'ap\test1',15,'2008-1-7' union all
select 'ap\test1',16,'2008-1-5' union all
select 'ap\test1',17,'2008-1-2' union all
select 'ap\test2',20,'2008-12-2' union all
select 'ap\test3',19,'2008-2-2'
go
select tb_a.account,name,c.date from tb_A inner join(
select * from tb_B b where not exists(select 1 from tb_B where account=b.account and bk_id>b.bk_id)) c on tb_a.account=c.account
select *
from tb_a as a join tb_b as b on a.ccount=b.account
where not exists(select 1 from tb_b where bk_id>b.bk_id)