H
H
happ2016-05-20 00:15:47
MySQL
happ, 2016-05-20 00:15:47

What is the best way to get user likes along with messages?

It is necessary to get information about whether the user who views the messages likes it. What is the best way to do this?
At the moment, a request has been written to receive posts and the number of likes.

SELECT `p`.`id`, `p`.`userId`, `p`.`message`, COUNT(`pl`.`postId`) as cnt_likes
FROM `posts` AS `p`
LEFT JOIN `post_likes` AS `pl` ON `pl`.`postId` = `p`.`id`
WHERE `p`.`userId` = :userId
GROUP BY `p`.`id`
ORDER BY `p`.`id` DESC

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2016-05-20
@happ

If there is a userid in post_likes, then add another join:
LEFT JOIN `post_likes` AS `plu` ON `plu`.`postId` = `p`.`id` and `plu`.`userId` = `p`. `userid`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question