Jump to content

Problem with GROUP BY


SamiBH

Recommended Posts

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  :D :

 

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  :shrug:

 

please if anyone can help me, I will be grateful

Link to comment
https://forums.phpfreaks.com/topic/221259-problem-with-group-by/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.