So I have 4 tables: users, posts, private, threads. In this example, lizzy created 2 private posts in different threads:
‘Dating’ post is for users 2,5,6 and herself to see the correct count of posts in that thread.
‘Break Ups’ post is for user 2 only and herself to see the correct count of posts in that thread.
Displaying the correct count depending on the user viewing the thread is the issue I am having. Here, we are focusing on lizzy, her threads, and post counts:
users (These aren't part of table. Just shows the counts we should display with our query depending on the user_id)
user_id | user_name //thread #: post_count-post_count_if_not_authorized = count to show
--------------------
1 | tony //thread 2: 3-1= 2 posts. thread 3: 2-1= 1 post.
2 | steph //thread 2: 3-0= 3 posts. thread 3: 2-0= 2 posts.
3 | lizzy //thread 2: 3 posts. thread 3: 2 posts.
4 | adam //thread 2: 3-1= 2 posts. thread 3: 2-1= 1 post.
5 | lara //thread 2: 3-0= 3 posts. thread 3: 2-1= 1 post.
6 | alexa //thread 2: 3-0= 3 posts. thread 3: 2-1= 1 post.
posts
post_id thread_id user_id post_name private (0 is public, 1 is private to authorized users)
-----------------------------------------------------
1 1 1 Coding 0
2 2 3 Dating 1
3 2 3 Show Me 0
4 2 3 See Me 0
5 3 3 Break Ups 1
6 3 3 True Love 0
private
private_id post_id authorized_user_id
-----------------------------------------------
1 2 2
2 2 5
3 2 6
4 5 2
threads
thread_id user_id post_count
------------------------------------
1 1 1
2 3 3 | When outputted in php, we should subtract the correct COUNT
3 3 2 | from this depending on the user viewing the thread like above.
So basically, we have a total thread count with all posts in that thread. But if we pull that out with a mysql query, all users will see all lizzy’s post_count for each thread she has, when instead, only lizzy and any users she authorized to view certain posts on a thread should see the correct visible non private count for them. What would be the most efficient way to pull out the counts as a row (post_count_if_not_authorized) so we can subtract it from the post_count to show each user the correct count for them only?
Something like the below is what I am after (not working of course as is):
SELECT DISTINCT t.thread_id, t.post_count, t.COUNT(*)
FROM threads as t
JOIN posts as p on p.user_id = t.user_id
LEFT JOIN private pv on pv.post_id = p.post_id
WHERE t.user_id='3'
AND (p.private = 0) OR (pv.authorized_user_id = {$logged_in_id} and p.private = 1)
UPDATE:
(t.user_id=’3′ in the WHERE clause is for lizzy in this example, and if $logged_in_id should give the correct count depending on the user, like the counts in the user table above)
Here is a fiddle.
The output should look like this if tony ($logged_in_id=1) is viewing lizzy’s (user_id=3) started threads:
thread_id post_count_final (this should have the correct count for this user($logged_in_id) to see, all posts by lizzy - private posts by lizzy that this user is not authorized to see)
2 2
3 1
If steph ($logged_in_id=2) is viewing lizzy’s (user_id=3) started threads:
thread_id post_count_final (this should have the correct count for this user($logged_in_id) to see, all posts by lizzy - private posts by lizzy that this user is not authorized to see)
2 3
3 2
(Note: The top right part next to the users table shows how these numbers are derived.)
This should work even if lizzy (logged in) is viewing lizzy. Adding COUNT(*) would return the same value as t.post_count. You could completely eliminate the use of threads table and do the counting in query all together, although the query will be more heavy.