select * from A inner join B a.guid=b.personid where datediff(d,b.birthday,a.birthday)/365<20
--如果只算年份. select a.* , (select personid , max(birthday) birthday from b group by personid) t where a.guid = t.personid and datediff(yy,t.birthday,a.birthday) < 20
declare @妇女表 table (guid uniqueidentifier,birthday varchar(20)) insert into @妇女表 select '76EC96F0-BE70-4648-9491-47DA12D8E351','1976-02-09' union all select '7ADE50D1-E389-4DC2-965A-D813291338BA','1991-02-09' union all select 'F08CF424-9B98-4A92-8DFC-7863CB367709','1982-02-09' union all select 'C48E8450-F8EF-4085-BD4F-76D2F7119FCC','1985-02-09' union all select '5EBC647C-8AEB-488B-8A19-F7C092D5805A','1979-02-09'declare @孩子表 table (id int,birthday datetime,personid varchar(36)) insert into @孩子表 select 1,'2010-02-12','5EBC647C-8AEB-488B-8A19-F7C092D5805A' union all select 2,'2009-12-02','7ADE50D1-E389-4DC2-965A-D813291338BA' union all select 3,'2010-12-02','F08CF424-9B98-4A92-8DFC-7863CB367709' union all select 4,'2010-12-08','C48E8450-F8EF-4085-BD4F-76D2F7119FCC' union all select 5,'2009-12-02','76EC96F0-BE70-4648-9491-47DA12D8E351'select a.* from @妇女表 a left join @孩子表 b on a.guid=b.personid where datediff(year,a.birthday,b.birthday)<20/* guid birthday ------------------------------------ -------------------- 7ADE50D1-E389-4DC2-965A-D813291338BA 1991-02-09 */
/*
3
*/
select a.* ,
(select personid , max(birthday) birthday from b group by personid) t
where a.guid = t.personid and datediff(yy,t.birthday,a.birthday) < 20
declare @妇女表 table (guid uniqueidentifier,birthday varchar(20))
insert into @妇女表
select '76EC96F0-BE70-4648-9491-47DA12D8E351','1976-02-09' union all
select '7ADE50D1-E389-4DC2-965A-D813291338BA','1991-02-09' union all
select 'F08CF424-9B98-4A92-8DFC-7863CB367709','1982-02-09' union all
select 'C48E8450-F8EF-4085-BD4F-76D2F7119FCC','1985-02-09' union all
select '5EBC647C-8AEB-488B-8A19-F7C092D5805A','1979-02-09'declare @孩子表 table (id int,birthday datetime,personid varchar(36))
insert into @孩子表
select 1,'2010-02-12','5EBC647C-8AEB-488B-8A19-F7C092D5805A' union all
select 2,'2009-12-02','7ADE50D1-E389-4DC2-965A-D813291338BA' union all
select 3,'2010-12-02','F08CF424-9B98-4A92-8DFC-7863CB367709' union all
select 4,'2010-12-08','C48E8450-F8EF-4085-BD4F-76D2F7119FCC' union all
select 5,'2009-12-02','76EC96F0-BE70-4648-9491-47DA12D8E351'select a.* from @妇女表 a left join @孩子表 b
on a.guid=b.personid
where datediff(year,a.birthday,b.birthday)<20/*
guid birthday
------------------------------------ --------------------
7ADE50D1-E389-4DC2-965A-D813291338BA 1991-02-09
*/