I am trying to get all the posts that belong to a certain user and then the like counts for each of those posts.
Here I get the all the posts that belong to a certain user:
SELECT Posts.id,
Posts.uuid,
Posts.caption,
Posts.path,
Posts.date,
USERS.id,
USERS.username,
USERS.fullname,
USERS.profileImage
FROM A.Posts JOIN A.USERS ON
Posts.id = 145 AND USERS.id = 145 ORDER by date DESC
LIMIT 0, 5
And here is how I query the likes for each post:
SELECT COUNT(uuidPost)
FROM Activity
WHERE type = "like" AND uuidPost = "FA4C8196-CEA3-4373-94B2-59F387BB1906"
Not sure how to combine them?
If anyone can help or give me tips on the queries, I'd appreciate all the help!
Thanks in advance!
SELECT Posts.id,
Posts.uuid,
Posts.caption,
Posts.path,
Posts.date,
USERS.id,
USERS.username,
USERS.fullname,
USERS.profileImage,
coalesce(A.LikeCNT,0),
IF( A.uuidPost IS NOT NULL , 1, 0 ) AS CurrentUser
FROM Posts
INNER JOIN USERS
ON Posts.id = 145
AND USERS.id = 145
LEFT JOIN (SELECT COUNT(A.uuidPost) LikeCNT, A.UUIDPost, A.id
FROM Activity A
WHERE type = 'like'
GROUP BY A.UUIDPOST) A
on A.UUIDPost=Posts.uuid
AND A.id = Posts.id
WHERE Posts.id = 145
ORDER BY date DESC
LIMIT 0, 5