So I have searched everywhere for this but I can't find this specific relation. The query below returns all posts from a user and the number of people who liked the post because I used a GROUP BY postid
meaning that if the result repeat themselves but have the same postid
then they are grouped.
SELECT posts.id postid,posts.post_body,posts.post_type, ALLUSERS.USERNAME,
likes.liker,likes.target,
plikers.*,
COUNT(posts.id) numberOflikes
FROM posts
INNER JOIN ALLUSERS ON(ALLUSERS.USERID=posts.FROM_userid)
LEFT JOIN likes ON(likes.target=posts.id)
LEFT JOIN(SELECT USERID pl_id FROM ALLUSERS )plikers ON(pl_id=likes.liker)
GROUP BY postid
And the result is...
+--------+-----------------+------------------------+-----------+-------+--------+-------+-----------+---------------+
| postid | post_body | post_type | USERNAME | liker | target | pl_id | pl_un | numberOflikes |
+--------+-----------------+------------------------+-----------+-------+--------+-------+-----------+---------------+
| 83 | Southgate | 20&&03 Saturday/04:05 | Superuser | NULL | NULL | NULL | NULL | 1 |
| 84 | Great post! | 10&&03 Saturday/04:07 | Superuser | 4 | 84 | 4 | dennisrec | 7 |
| 85 | How delightful? | 10&&03 Saturday/04:07 | Superuser | 43 | 85 | 43 | zerCon | 1 |
| 87 | Cheers... | 10&&07 Wednesday/01:53 | Superuser | NULL | NULL | NULL | NULL | 1 |
| 88 | check this out! | 20&&07 Wednesday/03:31 | Superuser | NULL | NULL | NULL | NULL | 1 |
+--------+-----------------+------------------------+-----------+-------+--------+-------+-----------+---------------+
Which is right but this only returns the first result of the grouped. So the quest stands, Is there any way to return all results of all groups in one query?
Now clearly I could just remove the GROUP BY
and the count(*)
phrase then get multiple duplicate results
then filter them to get all details of the likers
of the post but that would surely slow my servers down. So I've already tried that.
Any help would pretty much be greatly appreciated.