I’ve been working on this issue for awhile and I can’t seem to figure out a way to effectively do this.
Basically I have a table that holds questions and I have a table that keeps track of answers to the questions as well as the user info of the user associated with that answer. The answers are all true or false and I want to keep track of the total true or false answers per each question.
So the first query is selecting all records in the question table.
The second query selects the answers by questionID
I want to create a query that does all of this simultaneously, what I have most recently tried with no success is as follows:
SELECT questions.*, count(answers.*) as total_answers FROM questions
LEFT OUTER JOIN
(
SELECT answers.* AS true FROM votes
WHERE answer = 1 AND questionID = THIS.ID
)
This query doesn’t work because I can’t do another WHERE statement specifying that I want to only grab the records with the answer as 1 or 0 (the true or false answers are all binary). I haven’t found a THIS selector or anything similar in SQL. I hope I have been clear enough. I thought I had a pretty good understanding of Querying until I came across this issue.
Any help would be much appreciated
Refering to the answer Raphael mentioned, here is another way of writing that: