I have two table publications and publication_likes, where I have a many to many relationship and this fields: user_id, pub_id, created_at.
Users have a profile and on that profile they have publications. I need to get a specific user's publications, when his profile is requested(that's easy). Also I need to know which of those publications the user who is logged in likes.
Example: user_a is the profile owner and has publications, user_b is logged in and visits user_a profile. Server loads user_a publications and tells user_b which of those user_a publications has liked
Before asking I tried thinking and found a solution, but I don't know if it is correct
SELECT p.id,
p.user_id as creator,
p.p_content,
CASE WHEN pl.user_id = $1 THEN 1
ELSE 0
END as liked from publications p
LEFT JOIN publications_likes pl on p.id = pl.pub_id
WHERE p.user_id = $2;
Please tell me if you think this is right or not and if there is another way to do the same.
NOTE: I know the title it is not really specific but I could not think of other. If you can make it better, feel free to do it.
which of that publications the user who is logged in likes.Do you meanuser_ais logged in & looking at the profile ofuser_b, and you need to fetch all the publications liked byuser_bto show touser_aor publications byuser_bthatuser_ahas liked.user_awho has some publications, anduser_bwho may have likeduser_apublications and is logged in. I need to getuser_apublications and at the same time to know which from that publicationsuser_blikes. :-)$1=user who is viewing profile and liked publications&$2=user who created publication