Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:49:04+00:00 2026-06-02T20:49:04+00:00

Before I go on, I don’t want to use ANY query which involves selecting

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:49:06+00:00Added an answer on June 2, 2026 at 8:49 pm

    This is done with COUNT() aggregates, grouping by user_name.

    Get bans by user from most to least:

    SELECT
      user_name,
      COUNT(*) as numbans
    FROM bans
    GROUP BY user_name
    /* ordered by number of bans from greatest to least */
    ORDER BY numbans DESC
    

    Get only the most banned user:

    SELECT
      user_name,
      COUNT(*) as numbans
    FROM bans
    GROUP BY user_name
    ORDER BY numbans DESC
    /* adjust LIMIT for how many records you want returned -- 1 gives only the first record */
    LIMIT 1
    

    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 BY clauses 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

    SELECT
      user_name,
      COUNT(*) as numbans
    FROM bans
    GROUP BY user_name
    /* HAVING clause limits results of an aggregate like COUNT()  (which you cannot do in the WHERE clause) */
    HAVING numbans >= 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been hitting my head on the wall before as I don't make any test
Background: I've written this before but I don't like the approach. The reason is
I don't think this question has been asked before. I'm a bit confused on
I don't know if this question has been ask before. But I have a
I did a search before asking so please don't tell me to do that.
I don't understand what lifting is. Should I first understand monads before understanding what
I had never heard of persistent connections before, and I don't understand the advantages.
I'm looking around trying to see if anybody has asked this before. I don't
Apologies in advance but I have never seen this error before and don't know
i have studied design patterns and want to use them to code an open

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.