Fehrant Posted December 7, 2007 Share Posted December 7, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/ Share on other sites More sharing options...
Fehrant Posted December 7, 2007 Author Share Posted December 7, 2007 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-409340 Share on other sites More sharing options...
Fehrant Posted December 11, 2007 Author Share Posted December 11, 2007 *bump* Sorry to resort to bumping like this, but I'm still having issues. Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-412354 Share on other sites More sharing options...
mrdamien Posted December 11, 2007 Share Posted December 11, 2007 Sorry but I don't understand what the problem is. Is it not efficient enough? Did you want an INNER JOIN instead of a LEFT JOIN? Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-412365 Share on other sites More sharing options...
Fehrant Posted December 11, 2007 Author Share Posted December 11, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-412370 Share on other sites More sharing options...
wwelvaert Posted December 12, 2007 Share Posted December 12, 2007 Try using a WHERE clause instead of GROUP BY. Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-412377 Share on other sites More sharing options...
Fehrant Posted December 12, 2007 Author Share Posted December 12, 2007 I thought COUNT required GROUP BY. ??? Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-412402 Share on other sites More sharing options...
SirChick Posted December 12, 2007 Share Posted December 12, 2007 depends... if you group by it will count only the "diffent ones" rather than "all of them" group by: orange orange that equals 1 don't group by: orange orange that equals 2 Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-412404 Share on other sites More sharing options...
wwelvaert Posted December 12, 2007 Share Posted December 12, 2007 That was a braindamaged 30-second response of mine. You can't use WHERE but you can use HAVING, ie. something like this. SELECT Count(Product_Type_Table.Price) AS CountOfPrice FROM Product_Type_Table HAVING (Product_Type_Table.Price=60); Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-412823 Share on other sites More sharing options...
mrdamien Posted December 14, 2007 Share Posted December 14, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-415202 Share on other sites More sharing options...
corbin Posted December 14, 2007 Share Posted December 14, 2007 This would require a lot of altered coding, but it would definitely be less stressful on mysql.... You could always keep a post_count column, but you would have to be careful to keep it correct between adding, deleting so on with posts. Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-415206 Share on other sites More sharing options...
revraz Posted December 15, 2007 Share Posted December 15, 2007 You wouldn't have to bump so much if you posted this in the SQL forums instead of the PHP one. *bump* Sorry to resort to bumping like this, but I'm still having issues. Quote Link to comment https://forums.phpfreaks.com/topic/80697-sql-issue/#findComment-415277 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.