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

  • Home
  • SEARCH
  • 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 6048713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:29:39+00:00 2026-05-23T07:29:39+00:00

I have these 2 tables categories with fields id, category, total users with fields

  • 0

I have these 2 tables

categories with fields “id, category, total”
users with fields “id, category_id, title”

Category is a list of categories that a user can select.
Every time a user pick a certain category, we increment the total counter. (i.e popularity)

How can I grab a unique user data per category based ordered by categories.total and also the user.id needs to be the latest?

This is what I have. Almost there except this grabs the first user id.

select users.id from users left join categories on users.category_id = categories.id group by users.category_id order by categories.total desc

To illustrate, here is an example:
categories table has the following values
id = 1, category = free, total = 3
id = 2, category = expensive, total = 1
id = 3, category = cute, total = 2

users table has the following values
id = 1, category_id = 1, title = aello
id = 2, category_id = 2, title = bello
id = 3, category_id = 3, title = zello
id = 4, category_id = 1, title = gello
id = 5, category_id = 3, title = cello
id = 6, category_id = 1, title = fello

The sql query that I have returns the following users: 1, 3, 2 (the first user to be associated with the category that has the most total)

However, what I want the result to be is: 6, 5, 2 (the LAST user to be associated with the category that has the most total)

Thanks,
Tee

  • 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-23T07:29:40+00:00Added an answer on May 23, 2026 at 7:29 am

    Your requirements confuse me… so I’m not sure if this will really accomplish what you want… but it should return (6,5,2) (in no particular order). Add “ORDER BY category_id” or something, if ordering is important.

    SELECT MAX(id)
    FROM users
    GROUP BY category_id;
    

    To address your comment, try this (untested, as I don’t have MySQL installed anywhere):

    SELECT *
    FROM users
    WHERE id = (
        SELECT MAX(id)
        FROM users
        GROUP BY category_id
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.