Following are my two queries in which by adding only one more left join result gets messed up. 2nd query result is accurate but in 1st query by adding one more left join output is not right (as you can see i only added one more left join but i didnot put any filteration for that join in where clause) . Kindly let me know how can i fix this? Thanks,
1st Query:
SELECT Distinct count(event_id) as event_count from events
Left Join events on events.event_id = my_events.i_event_id
Left Join atdees on events.event_id = atdees.fk_event_id
where my_events.v_title != "NULL" and r_present = 1 and resident_id = '208' and event_atd > date_sub(curdate(), interval 37 day) group by event_count order by event_count desc limit 5
Result:
26 | 12 | 11 | 10
2nd Query:
SELECT Distinct count(event_id) as event_count from events
Left Join events on events.event_id = my_events.i_event_id
where my_events.v_title != "NULL" and r_present = 1 and resident_id = '208' and event_atd > date_sub(curdate(), interval 37 day) group by event_count order by event_count desc limit 5
Result:
2 | 1 | 1 | 1
What the left join does is that it adds extra (duplicate) rows.
If you strip out the
countand just list the rows, you’ll see many duplicate rows.This is because you’re asking for the cross product between
eventsandatdees; and of course there are many more combinations of events and atdees then there are just events.Basic logic really.
Change your top query to
And you should be getting the same results (although this is much slower)
Further note that
left joins do not filter, they add stuff.If you want to filter you’d use
inner joins(This is a gross oversimplification)
About
distinctThe
distinctkeyword can work inside and outside of an aggregate function.If you use it inside, it will only count (sum etc) unique values.
If you use it outside the aggregate function it will only list unique rows.
That is:
distincton its own eliminates duplicate rows (it works on all columns in the result set).distinctin a function works as a filter for that function only.