Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join 3 tables and count number of rows from the last one 1

Status
Not open for further replies.

Smarty

Programmer
Apr 12, 2001
191
BE
I have 3 tables:
1st table: SURVEY (surveyid, surveyquestion,...)
2nd table: OPTION (surveyid, optionid, optionname,...)
3rd table: ANSWER (surveyid, optionid, answerid, answertime,...)

With this 3 tables, I have a 'Question of the week' on my webpage. Filling these tables (ANSWER for the votes) is not a problem, but I want a query for the result with these fields:
Surveyquestion, Optionname, number of answers for this surveyid and optionid.
So that I have in 1 query all my desired fields, and that I can code
Survey 1:
- Option 1 (34 votes)
- Option 2 (59 votes)
...

Can you help me with this query? I would start from the OPTION table, do an inner join on SURVEY, but the count(answerid) is not completly clear to me.

Thanks in advance,

(not so) Smarty
 
SELECT S.surveyquestion, o_Optionname, COunt(*) AS CountOfVotes
FROM (ANSWER A
INNER JOIN SURVEY S ON A.surveyid = S.surveyid)
INNER JOIN OPTION O ON A.surveyid = O.surveyid AND A.optionid = o_Optionid
GROUP BY S.surveyquestion, o_Optionname

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top