Jump to content

Recommended Posts

Consider the following tables (for simplicities' sake I have used only the fields that are relevant):

 

threads (fields: threads_id)

posts (fields: posts_id, fk_threads, fk_users)

users (fields: users_id, username)

 

The issue: while posts are looped with all their respective information accordingly to which thread they belong (fk_threads -> threads_id), all the user information is also listed in each post box. Having successfully completed this, I decided to add more piece of user information, namely post count. Obviously, in order to obtain the total post count of a specific user, one would have to use COUNT on all the posts that have a matching fk_users value.

 

After trying for a while to figure out if I could make both the listing of posts with the user information, and the post count altogether in the same query, I gave up, and realized the post count would merit a separate query of its own. But even with this, I quickly came into some issues.

 

The best query I could came up with at the moment was this one:

 

SELECT users.users_id, COUNT(posts.posts_id) AS u_postcount
FROM posts
LEFT JOIN users ON posts.fk_users = users.users_id
GROUP BY users.users_id;

 

The result of this query is all the users, with all their respective post counts. However, this would be a waste of resources, as in a single thread, not every single user in the forum necessarily posted.

 

Storing the results in an array and then using an if to check if the user_id matches to show (or not) the post count was the best I could come up with at the moment.

 

Got any better ideas?

Link to comment
https://forums.phpfreaks.com/topic/80697-sql-issue/
Share on other sites

The problem is that the current query pulls up all the users, and all their corresponding post counts. Say, in this very post, the only users are you and me. So why would I bother stuffing an array with every single user post count for this post, if it's only you and me that posted? That's inefficient.

 

However, I can't seem to find the right query, or maybe I'm not facing the issue properly.

Link to comment
https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-412370
Share on other sites

If I understand correctly, you'll want a query similar to something like this:

 

SELECT users.user_id, COUNT(posts.post_id) AS u_postcount

FROM users

INNER JOIN posts ON users.user_id = posts.fk_users

INNER JOIN threads ON posts.fk_threads = threads.threads_id

WHERE threads.threads_id = ?

GROUP BY users.user_id;

 

meaning select the users and their postcount, but only the users that have posted on threads_id

Link to comment
https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-415202
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.