SamiBH Posted December 10, 2010 Share Posted December 10, 2010 Hello, I'm having problem and I really need your help My Tabels: Posts: Users: categories: PostID UserId CatID UserID Username CatName I want to list Top poster in every category. Like: cat1 user1 1000 posts cat2 user2 900 posts cat3 user1 1000 posts so, I tried to do it : SELECT p.UserID, c.id, u.username, COUNT(PostID ) FROM Posts p LEFT JOIN users u ON u.id = p.UserID LEFT JOIN categories c ON c.id = p.category GROUP BY c.id, u.Username ORDER BY COUNT(PostID ) DESC LIMIT 27 and the output: UserID id username COUNT(PostID) 11886 45 user1 1299 11885 45 user2 756 11886 45 user1 751 11886 42 user1 491 as you can see there is 3 (cat id) repeated please if anyone can help me, I will be grateful Quote Link to comment https://forums.phpfreaks.com/topic/221259-problem-with-group-by/ Share on other sites More sharing options...
fenway Posted December 11, 2010 Share Posted December 11, 2010 That's because you can't mix JOINs and GROUP BY that way -- and you can't rely on any non-aggregated columns, either. Quote Link to comment https://forums.phpfreaks.com/topic/221259-problem-with-group-by/#findComment-1145900 Share on other sites More sharing options...
SamiBH Posted December 12, 2010 Author Share Posted December 12, 2010 That's because you can't mix JOINs and GROUP BY that way -- and you can't rely on any non-aggregated columns, either. thank you for your reply so what should I do? Quote Link to comment https://forums.phpfreaks.com/topic/221259-problem-with-group-by/#findComment-1146086 Share on other sites More sharing options...
fenway Posted December 12, 2010 Share Posted December 12, 2010 Drop the joins, get the desired grouped result first, then join. Quote Link to comment https://forums.phpfreaks.com/topic/221259-problem-with-group-by/#findComment-1146318 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.