Solved

MYSQL Select Statement to Return Groups of Ages?

Posted on 2004-08-16
2
745 Views
Last Modified: 2006-11-17
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
>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?

Thanks!
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.

Richard
0
Comment
Question by:rcbuchanan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
andreif earned 500 total points
ID: 11815665
Hi, here is my solution

1. create extra table for age groups:

create table agegroups (startage int, endage int, title varchar(25));
and add all intervals there, like this:
18, 25, '18-25'
26, 35, '26-35'
...
66, 999, '>65'

2. now your select will look like this:

SELECT ag.title, Count(*)
FROM opinionvotes o, users u,  demographics  d
LEFT JOIN agegroups ag ON (year(now()) - d.birthyear - 1900 between ag.startage and ag.endage);
WHERE o.userID = u.userID
AND u.userID = d.userID
AND o.opinion_id = 232
GROUP BY ag.startage
0
 

Author Comment

by:rcbuchanan
ID: 11815859
wondeful! thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP: Insert Data into MySQL 5 62
Currency in SQL? 2 45
Problem with MySQL query - graph 3 24
How to use 2 ON statements in inner join 3 23
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question