MYSQL Select Statement to Return Groups of Ages?
Posted on 2004-08-16
Can you help? I have this working mySQL Select statement that returns a ONE ROW result ... that counts the number of users between the age of 18 and 25.
SELECT Count(*) AS CountOf18to25s
FROM opinionvotes o, users u, demographics d
WHERE o.userID = u.userID
AND u.userID = d.userID
AND o.opinion_id = 232
AND (d.birthyear + 1900) BETWEEN (Year(Now()) - 25) AND (Year(Now()) - 18)
I ALSO need to find out how many users are between these age bands ALSO:
26 to 35
36 to 45
46 to 55
56 to 65
RATHER than performing 6 seperate SELECT statements ... ONE for EACH of these AGE bands (including the 18 to 25) ... I'm HOPING a Select Statement can be written that will return 6 ROWS ... with ONE ROW for EACH of the above age bands. IS this possible?
Each 'record' in the demographics table contains a field called 'birthyear' which is the TWO DIGIT YEAR of BIRTH! SO I need to ADD 1900 to the YEAR i.e. 65 + 1900 = 1965 and SUBTRACT the 1965 from the current year (2004) to get 39 years of age.