在SQL中,年龄表达问题! (50分)

  • 主题发起人 主题发起人 stone_syj
  • 开始时间 开始时间
S

stone_syj

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾,帮小弟一把!

表BASE。DB中有字段BIRTHDAY。
现在我想统计年龄20-30岁的人的记录的数目!
SQL该如何写法????
这里的BIRTHDAY是DATE类型的!!!

请帮帮忙!!!
 
select birthday from ...
where birthday>=20 and birthday<=30
 
select * from Table where birthday>19 and birthday<31
 
select birthday from table where datediff(year,getdate(),birthday) between 20 and 30
 
select count(*) from base
where birthday > '1/1/70' and birthday < '12/31/80'
 
如果birthday 是出生日期
select count(*) from base where birthday > '1/1/70' and birthday < '12/31/80'
如果是年龄
select count(*) from base where birthday between 20 and 30
 
可不可以这样:
select count(*) from base
where now-birthday between 20 and 30
 
燕子,我不知道在SQL语句中有哪些函数可以使用!
你能告诉我吗??要是有这方面的电子书,发email给我!
我的email是:iis2000@263.net
非常感谢!!!
 
to 燕子:
now-birthday的结果是两者相差的秒数(可能是毫秒),直接用不行,用year()函数处理
一下才能用
 
to 追命:
 year()函数能在SQL语句中用吗?getdate()我试了一下,不行呀!
 不知道有没有有关SQL的书呀!!!!!
 知道的话告诉我好吗?
 多谢!!!!
 
装一个M$ Access(要带帮助一块装)
里边有说明和例子,差不多也能对付了。DateDiff

select count(birthday) from base
where datediff('yyyy',birthday,now()) between 20 and 30
 
to 追命:那这样写对吗?
select count(*) from base where (year(now())-year(birthday)) between 20 and 30
 
Select Count(*) from base
where 2000-birthday between 20 and 30
其中的2000可用参数的方式赋值
 
SELECT COUNT(*) FROM BASE
WHERE BIRTHDAY+30>=:NOW AND BIRTHDAY+20<=:NOW
其中:now为参数
 
这么激烈!!
 
to 燕子
select count(*) from base where datediff(yy,birthday,now) between 20 and 30
 
你可以在程序中计算一下出生日期范围:
starttime,endtime:Tdatetime;
starttime:=now-30;
endtime:=now-20;
'select * from table1 where birthday between '+quotedstr(timetostr(starttime))
+ ' and ' + quotedstr(timetostr(endtime))
 
更正:
starttime,endtime:tdatetime;
year,day,month :Word;
DecodeDate(now,Year, Month, Day);
starttime:=EncodeDate(Year-30, Month, Day);
starttime:=EncodeDate(Year-20, Month, Day);
'select * from table1 where birthday between '+quotedstr(timetostr(starttime))
+ ' and ' + quotedstr(timetostr(endtime))
 
系统日期减去出生日期,查询便可
 
后退
顶部