a表 妇女表 guid:自增长
                  birthday:出生日期
b表 孩子表 id:主键
                  birthday:出生日期
                  personid:母亲,a表外键
怎么查询出子女出生时育龄妇女年龄小于20岁的所有妇女的信息(a表)

解决方案 »

  1.   

    select datediff(d,'2008-05-20','2012-01-24')/365
    /*
    3
    */
      

  2.   

    select * from A inner join B a.guid=b.personid where datediff(d,b.birthday,a.birthday)/365<20
     
      

  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 
      

  4.   


    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
    */