I'm building a forum software and am trying to grab all the posts by a particular 'profile', grab all of the profiles that have 'liked' each post ("likers"), followed by a third fetch of the profile data of each profile that has liked each post (profile_name
, etc.).
Table 1 - posts
---------------
post_id
profile_id (original poster)
content
...
Table 2 - like
--------------
profile_id
post_id
Table 3 - profiles
------------------
profile_id
profile_name
...
Some rules:
- Each like is simply a relation of the
post_id
andprofile_id
- A profile cannot like a post more than once
- A profile cannot like their own post
I'm stuck on the step where I store all the like profile_ids ("likers") into an array, if using PHP to fetch the data. Here is what I have so far:
SELECT post.*, COUNT(`like`.profile_id) AS like_count
FROM post LEFT OUTER JOIN `like` ON post.post_id = `like`.post_id
WHERE post.profile_id = "'.$this->profile_id.'" GROUP BY post.post_id
Thanks in advanced. I'll try and update this if I find any mistakes, or if any are pointed out to me.