开发者

MySQL date precision

I have this on my WHERE clause

dob <= DATE_SUB(CURDATE(),INTERVAL 1 YEAR) AND dob >= DATE_SUB(CURD开发者_如何学GoATE(),INTERVAL 19 YEAR) 

It works alright,it searches a persons by age based on their DOB, however there is some precision issues.

Consider the following:

1 to 19                                 =             397
20-25                                   =            1350
26- 30                                  =            1139
31-35                                   =             601
36-40                                   =             346
41-45                                   =             218
46- 50                                  =             154
51-55                                   =             139
56-60                                   =              65
61-85                                   =              78      Total = 4487

If I use the same method but search for 1 - 85 return 5607

I am puzzled but then again dates are my weakness so any help would be appreciated.


Oh, I think when you do DATE_SUB(CURDATE(),INTERVAL 19 YEAR) and then DATE_SUB(CURDATE(),INTERVAL 20 YEAR)

You miss people in there 19th years, so you're missing the people in there 19th, 26th, 30th, 35th ... etc

It should be dob < DATE_SUB(CURDATE(),INTERVAL 1 YEAR) AND dob >= DATE_SUB(CURDATE(),INTERVAL 19 YEAR)

And then dob < DATE_SUB(CURDATE(),INTERVAL 19 YEAR) AND dob >= DATE_SUB(CURDATE(),INTERVAL 25 YEAR)

edit :

 1  to 19                                   =            ??
 19 to 25                                   =            ??
 25 to 30                                   =            ??
 30 to 35                                   =            ??
 35 to 40                                   =            ??
 40 to 45                                   =            ??
 45 to 50                                   =            ??
 50 to 55                                   =            ?? 
 55 to 60                                   =            ??
 60 to 85                                   =            ??`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜