菜鸟求一条sql语句,从只有生日的表里查出年龄在31岁到35岁之间的数据,高手出现吧!! ( 积分: 50 )

  • 主题发起人 wanglong1
  • 开始时间
W

wanglong1

Unregistered / Unconfirmed
GUEST, unregistred user!
这是我的表:
id 姓名 生日
1 刘德华 1990-12-12
2 张学友 1975-12-12
现在的时间是2007年6月6日,我想求出31岁到35岁之间的人的数据,怎么做呢??
 
select * from 表
where year(生日) - year(now()) between 31 and 35
 
sql语句这样写
select * from table1 where (生日>=:d0) and (生日<=:d1)
然后:
Params.ParamByName('d0').AsDateTime := IncYear(date,-35);
Params.ParamByName('d1').AsDateTime := IncYear(date,-31);
然后
open;
 
用 datediff函数怎么样?
 
select * from tab where datediff(year,生日,getdate()) between 31 and 35
 
1.select * from 表 where year(getdate()) -year(生日) between 31 and 35
2.select * from 表 where datepart(year,getdate()) -datepart(year,生日) between 31 and 35
3.select* from tab where datediff(year,getdate(),生日) between 31 and 35
 
楼上几位的回答只考虑了年,没有将月,日部分考虑进去。
Select * From Table
Where (DatePart(Year, Birthday) - DatePart(Year, GetDate()) Between 31 And 35) And (
(DatePart(Month, Birthday) = DatePart(Month, GetDate())) AND
(DatePart(Day, Birthday) <= DatePart(Day, GetDate())) OR
(DatePart(Month, Birthday) < DatePart(Month, GetDate()))
)
 
顶部