25

I am trying to get the number of page opens on a per day basis using the following query.

SELECT day.days, COUNT(*) as opens 
FROM day 
LEFT OUTER JOIN tracking ON day.days = DAY(FROM_UNIXTIME(open_date)) 
WHERE tracking.open_id = 10 
GROUP BY day.days

The output I get it is this:

days opens
1   9
9   2

The thing is, in my day table, I have a single column that contains the number 1 to 30 to represent the days in a month. I did a left outer join and I am expecting to have all days show on the days column!

But my query is doing that, why might that be?

1

3 Answers 3

74

Nanne's answer given explains why you don't get the desired result (your WHERE clause removes rows), but not how to fix it.

The solution is to change WHERE to AND so that the condition is part of the join condition, not a filter applied after the join:

SELECT day.days, COUNT(*) as opens 
FROM day 
LEFT OUTER JOIN tracking
ON day.days = DAY(FROM_UNIXTIME(open_date)) 
AND tracking.open_id = 10 
GROUP BY day.days

Now all rows in the left table will be present in the result.

2
  • 2
    alternatively, the where clause could have been changed to WHERE (tracking.open_id IS NULL or tracking.open_id = 10) although i much prefer putting the predicate in the join clause like mark did, as I feel it makes most sense there.
    – goat
    Commented Jan 16, 2011 at 20:04
  • 3
    @chris: That works when the join fails, but won't work in the case where the join succeeds but the joining row has tracking.open_id equal to something other than 10. In this case the row will still be removed.
    – Mark Byers
    Commented Jan 16, 2011 at 20:06
16

You specify that the connected tracking.open_id must be 10. For the other rows it will be NULL, so they'll not show up!

2
  • @Nanne I think you are mistakable +1 Commented Apr 12, 2012 at 8:18
  • IMPORTANT In case people are like me and don't scroll past the accepted answer, the response below this has a much better explanation and shows how to get OP's desired result as well as how to fix the problem Commented Sep 16, 2019 at 21:33
3

The condition is in the WHERE clause. After joining the tables the WHERE conditions are evaluated to filter out everything matching the criteria.Thus anything not matching tracking.open_id = 10 gets discarded.

If you want to apply this condition while joining the two tables, a better way is to use it with the ON clause (i.e. joining condition) than the entire dataset condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.