LEFT JOIN variation based on dates
I have a series of tables related to "Polls", they store information regarding them, their answers, votes, style, etc.
There's a page in which I display a report of all the information regarding a single poll, including the answers of that poll, the number of votes on each answer and even the number of votes of the "inactive" answers (The ones not being displayed in the poll at the moment). To extract that information I use the following query:
SELECT answers.id, answers.answer_text, COUNT(polls_results.id) as total
FROM answers LEFT JOIN polls_results
ON answers.id = polls_results.answer_id
WHERE answers.poll_id = ? AND answers.activate = '0'
GROUP BY answers.id
Where "?" is changed for the ID when the query is prepared. This query returns all the information I need, for example:
[Name of the Poll]
[Poll Question]
Active Answers
- Answer 1 [# of votes]
- Answer 2 [# of votes]
- Answer 3 [# of votes]
Inactive Answers
- Answer 4 [# of votes]
- Answer 5 [# of votes]
Now, regarding the qu开发者_如何学Pythonestion itself, the report has an option to filter the answers based on a range of dates, could be a starting date, an end date or both, when this happens, the query changes adding only this:
After this change's been made, if there is an answer, active or inactive, with zero votes, the query simply won't show it, unlike the query without the date range.
I've found no explanation to this, I've tried a couple variations of this query but I believe it works correctly, I just don't understand why it changes so drastically with the dates.
Does any one have an idea or lead as to why this is happening?
Edit:
As requested, here's the final query with the AND clause
SELECT answers.id, answers.answer_text, COUNT(polls_results.id) as total
FROM answers LEFT JOIN polls_results
ON answers.id = polls_results.answer_id
WHERE answers.poll_id = ? AND answers.activate = '0'
AND polls_results.date >= ".$startDate." AND polls_results.date <= ".$endDate."
GROUP BY answers.id
polls_results is in left join clause, so it is subject of having records which will have nulls .... now when we put condition in where, records with null will always fail, as where filter is applied after join has been evaluated, hence there would be no meaning of left join, it will work as inner join.
Try putting this And clause in Join condition, instead of where clause ....
SELECT answers.id, answers.answer_text, COUNT(polls_results.id) as total
FROM answers
LEFT JOIN polls_results ON answers.id = polls_results.answer_id
AND polls_results.date >= ".$startDate." AND polls_results.date <= ".$endDate."
WHERE answers.poll_id = ?
AND answers.activate = '0'
GROUP BY answers.id
Also you can use a subquery instead of join:
SELECT answers.id, answers.answer_text, (select count(*) from polls_results where
polls_results.answer_id = answers.id and polls_results.date >= ".$startDate." AND polls_results.date <= ".$endDate."
) as total FROM answers where answers.poll_id = ? AND answers.activate = '0'
精彩评论