I am trying to build a feed for a photography site I am building. Below is the query to get all activity that has happened in the previous DAY. This works perfectly if there is a comment left in the last day(the first LEFT OUTER JOIN) but if only likes have happened(the second LEFT OUTER JOIN) then this returns empty. I think the problem is if the first LEFT OUTER JOIN is empty then the second one seems to not fire. If I am right in this assumption do you have any idea how to get the second to fire regardless of the outcome of the first.
SELECT a.a_id
, a.a_user_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 1 DAY)
AND
COALESCE(c.ic_creator_id, s.photographer_id) != a.a_user_id
ORDER BY a.a_activity_time DESC
UPDATE: Upon further testing it seems the COALESCE() in the WHERE clause is the problem. One of these fields will always have a value while the other will not. If I get rid of this the query works fine which is good because I can use it as is and then in the application remove what I need to remove but I would still really like this to work in the query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.