943,569 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 1399
  • MySQL RSS
Jun 18th, 2009
0

help with (hopefully) simple nested query

Expand Post »
I'm writing a query to summarize survey results. There are two tables...

1) The Profiles table contains the users' answers. There are four columns: ProfileID (for the unique key), QuestionID (a number representing canned questions from another table), Answer (a number representing multiple choice answers from another table), and UserID.

2. The Users table has three columns that apply to the query: UserID (the same as that in the Profiles table) and Gender.

What I want to do is present a tally of how many people answered a question in a particular way. For instance, if I wanted to list people's favorite ice cream flavors, I'd want to see:
CHOCOLATE 7
VANILLA 4
ETC.
With QuestionID 4 corresponding to the Ice Cream Flavor question, that was pretty straight-forward with...

MySQL Syntax (Toggle Plain Text)
  1. SELECT Profiles.Answer, COUNT(Profiles.Answer) FROM Profiles, Users
  2. WHERE Profiles.QuestionID =4 AND Users.UserID = Profiles.UserID
  3. GROUP BY Profiles.Answer;

Now what I want to is group the results by Gender...
CHOCOLATE
MALE 4
FEMALE 3
VANILLA
MALE 2
FEMALE 2
How do I write the nested query to group the first query's results by Gender?

Thanks in advance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mandler is offline Offline
7 posts
since Jun 2009
Jun 19th, 2009
0

Re: help with (hopefully) simple nested query

Try that

SELECT     Profiles.answer, users.gender, COUNT(Profiles.answer) AS count
FROM         Profiles INNER JOIN
                      users ON users.userid = Profiles.userid
GROUP BY users.gender, Profiles.answer

Regards.
Reputation Points: 56
Solved Threads: 56
Posting Pro in Training
jbisono is offline Offline
431 posts
since May 2009
Jun 19th, 2009
0

Re: help with (hopefully) simple nested query

It's perfect.
Thank you very much.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mandler is offline Offline
7 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Rows affected in mysql
Next Thread in MySQL Forum Timeline: help wif uploading My sql database into Phpmyadmin.com





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC