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 814937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:36:09+00:00 2026-05-15T01:36:09+00:00

I am making a website where users can vote on which category a page

  • 0

I am making a website where users can vote on which category a page is. They can vote that the page is in category a, b, c, or d.

I need to find the most commonly occurring category in the MySQL row out of all the votes.

Each time a user submits their vote, it submits the “category” that they voted for, and the “page_id”.

I have this so far:

SELECT    page_id, category
FROM      categories
GROUP BY  page_id

I cannot use a COUNT(*) WHERE category = 'a' then repeat it for each category because there is many more categories in the actual project.

  • 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-05-15T01:36:10+00:00Added an answer on May 15, 2026 at 1:36 am

    If your table looks something like this:

    SELECT * from categories;
    +---------+----------+
    | page_id | category |
    +---------+----------+
    |       1 | a        |
    |       1 | b        |
    |       1 | a        |
    |       1 | c        |
    |       1 | a        |
    |       1 | b        |
    |       1 | a        |
    |       2 | d        |
    |       2 | d        |
    |       2 | c        |
    |       2 | d        |
    |       3 | a        |
    |       3 | b        |
    |       3 | c        |
    |       4 | c        |
    |       4 | d        |
    |       4 | c        |
    +---------+----------+
    17 rows in set (0.00 sec)
    

    Then you may want to try this query:

    SELECT   c1.page_id, MAX(freq.total),
             (
                SELECT   c2.category
                FROM     categories c2
                WHERE    c2.page_id = c1.page_id
                GROUP BY c2.category
                HAVING   COUNT(*) = MAX(freq.total)
                LIMIT    1
             ) AS category
    FROM     categories c1 
    JOIN     (
                SELECT   page_id, category, count(*) total 
                FROM     categories 
                GROUP BY page_id, category
             ) freq ON (freq.page_id = c1.page_id) 
    GROUP BY c1.page_id;
    

    Which returns this:

    +---------+-----------------+----------+
    | page_id | MAX(freq.total) | category |
    +---------+-----------------+----------+
    |       1 |               4 | a        |
    |       2 |               3 | d        |
    |       3 |               1 | a        |
    |       4 |               2 | c        |
    +---------+-----------------+----------+
    4 rows in set (0.00 sec)
    

    Compare the results with the actual frequency distribution:

    SELECT page_id, category, COUNT(*) FROM categories GROUP BY page_id, category;
    +---------+----------+----------+
    | page_id | category | COUNT(*) |
    +---------+----------+----------+
    |       1 | a        |        4 |
    |       1 | b        |        2 |
    |       1 | c        |        1 |
    |       2 | c        |        1 |
    |       2 | d        |        3 |
    |       3 | a        |        1 |
    |       3 | b        |        1 |
    |       3 | c        |        1 |
    |       4 | c        |        2 |
    |       4 | d        |        1 |
    +---------+----------+----------+
    10 rows in set (0.00 sec)
    

    Note that for page_id = 3, there is no leading frequency, in which case this query makes no guarantee on which category will be chosen in such a case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was making a website for a music band, and i was wondering the
I am thinking about making a website with some fairly intense JavaScript/canvas usage and
I have a question about tables in MySQL. I'm currently making a website where
Right now I'm making an extremely simple website- about 5 pages. Question is if
I'm creating a website for my church and I'm having problems making it display
I am currently in the process of making a new ASP.net MVC website, and
I'm making my way through the early Data Access Tutorials on Microsoft's ASP.NET website
This is beyond both making sense and my control. That being said here is
I've rewritten my family web site using JavaScript (JQuery) making Ajax calls to PHP
We use ASP.NET, C# When making an update to one of our websites, we

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.