I am building a user feed for members of a photography site I created and this query grabs all of the activiy in the last 24 hours so I can send emails to the users. The issue I am having is if the first LEFT OUTER JOIN has no results then the query returns no results even if the second JOIN contains results. I am pretty sure I have narrowed this down to the WHERE clause and specifically the COALESCE() portion. I thought that if this function received only one value it would use it. PLease let me know what you think or if you have another solution.
SELECT a.a_id
, a.a_us er_id
, a.a_activity_type
, a.a_activity_id
, a.a_activity_time
, c.ic_id
, c.ic_status
, c.ic_image_id
, c.ic_creator_id
, c.ic_comment
, c.ic_read_status
, s.id
, s.image_id
, s.photographer_id
, s.user_id
, u.u_id
, u.u_screen_name
, u.u_url_screen_name
, u.u_email
, u.u_avatar
, u1.u_id as currentUser
, u1.u_email as currentEmail
, u1.u_url_screen_name as currentScreenName
, i.i_id
, i.i_name
, COALESCE(c.ic_image_id, s.image_id) as image_id
, COALESCE(c.ic_creator_id, s.photographer_id) as other_user_id
FROM activity AS a
LEFT OUTER JOIN image_comments as c ON
a.a_activity_id = c.ic_id AND a.a_activity_type = 1
LEFT OUTER JOIN site_likes as s ON
a.a_activity_id = s.id AND a.a_activity_type = 2
LEFT JOIN users as u ON
u.u_id = COALESCE(c.ic_creator_id, s.photographer_id)
LEFT JOIN images as i ON
i.i_id = COALESCE(c.ic_image_id, s.image_id)
LEFT JOIN users as u1 ON
u1.u_id = a.a_user_id
WHERE a.a_activity_time >= DATE_SUB(now(), INTERVAL 24 HOUR)
AND
COALESCE(c.ic_creator_id, s.photographer_id) != a.a_user_id
ORDER BY a.a_activity_time DESC