Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9255693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:46:06+00:00 2026-06-18T11:46:06+00:00

So I have 4 tables: users, posts, private, threads. In this example, lizzy created

  • 0

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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:46:08+00:00Added an answer on June 18, 2026 at 11:46 am
    SELECT 
    t.thread_id, t.post_count, 
    COUNT(IF(ISNULL(pv.private_id) AND p.private='1' AND p.user_id<>'1', null, 1)) 
    FROM threads as t
    JOIN posts as p on p.thread_id = t.thread_id 
    JOIN users as u on u.user_id = p.user_id 
    LEFT JOIN private as pv on pv.post_id = p.post_id AND pv.authorized_user_id='1' 
    JOIN users as auth on auth.user_id = '1'
    WHERE p.user_id='3' AND t.user_id='3'
    GROUP BY t.thread_id;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have 3 tables: users, posts, private. In this example, lizzy created a
I have four tables: users : id, thread_id threads : id, language_id posts :
In my application I have the following tables: Posts id title content Users id
I have 3 tables: - users - posts - Subscriptions (which should be by
I have users and posts tables and I want to set it up so
I have two tables: posts - holds post information listen - holds information on
I have Two diff tables as given below: users and posts Need data from
I have 2 tables, one called posts and one called users . Inside posts
I have two tables: users and posts . I'm trying to select any users
I have a Database with the following two tables, USERS, POSTS I am looking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.