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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:10:14+00:00 2026-05-20T15:10:14+00:00

I have a user table with id, username and food_id columns. The idea is

  • 0

I have a user table with id, username and food_id columns. The idea is the user stored their favorite food and we come up with a league table of foods and I want to generate a report of the top votes for the each food type. I am using MySQL and PHP.

For clarity, here is an example of the table:

 id  food_id   username
 1   1         Bob
 2   100       Jane
 3   200       Andy
 4   1         Maggy
 5   100       Rich
 6   100       Mick
 7   1         Kevin

How do I write the query to list the foods that have the most votes. I want to limit the result to x number, say top 100. In this case I want the result to be as follows:

food_id score
1 3
100 4

I hope the question is clear enough. The query is beyond me but I am sure it must be possible to do it using DISTINCT and COUNT in some way or other. Perhaps it’s sub queries?

  • 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-20T15:10:14+00:00Added an answer on May 20, 2026 at 3:10 pm
    select food_id, count(*) score
       from myTable
    group by food_id
    order by score desc limit 100
    
    
    
    SELECT F.food_name,
        COUNT(*) AS score
    FROM myTable AS M 
        INNER JOIN food_table AS F
            ON F.food_id = M.food_id
    GROUP BY F.food_name
    ORDER BY score DESC limit 100
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of users with three columns. Username Accepted Rejected User 1
I have a User table where there are a Username and Application columns. Username
I have a user table with userid and username columns, and both are unique.
In SQL Server 2008 I have a user table like the following: Userid Username
I have two tables, a user table and a application table: User id username
I have a User table that has all of their avatars saved in an
Say I have three tables: User Table { UserId INT, Username NVARCHAR ... }
We have a user table, every user has an unique email and username. We
I have a user table structured like this: id MEDIUMINT(7), username VARCHAR(15) Would it
I have a User table, which is having Userid(primary key), Username, Password There is

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.