Before I go on, I don’t want to use ANY query which involves selecting all rows and counting the occurrences manually. I’m doing this in PHP by the way.
Basically, I have a bans table. Each new record/row is a new ban. The field titled user_name signifies which player was banned. Is there a way to count who has the most bans in this table? I really don’t want to select every row and then count it out for each player. The table is pretty big, therefore making the mentioned solution impractical and inefficient.
This is done with
COUNT()aggregates, grouping byuser_name.Get bans by user from most to least:
Get only the most banned user:
In your question, you’re very concerned about not wanting to count manually in PHP. Note that this is just about never necessary. RDBMS systems are designed explicitly for organizing and querying data, and are very good at doing tasks like this efficiently. Read up on
GROUP BYclauses and aggregate functions in MySQL and master them. You generally shouldn’t need to do it in code.Bonus: Get all users banned 3 or more times