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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:25:56+00:00 2026-05-24T20:25:56+00:00

I need some help with a group by mysql query clause. Medals Table (this

  • 0

I need some help with a group by mysql query clause.

Medals Table (this holds all the medals):

+-----------------------------------------------------------------------+
| medal_id | medal_level | medal_name      | medal_type | medal_icon    |
|-----------------------------------------------------------------------|
|        1 |           1 |    1 post medal |          1 | icon_file.png |
|        2 |           2 |  1 thread medal |          2 | icon_file.png |
|        3 |           1 |    2 post medal |          1 | icon_file.png |
|        4 |           2 | 2 threads medal |          2 | icon_file.png |
+-----------------------------------------------------------------------+

Users Medals Table (this holds the medals which users have won):

+--------------------------------+
|medal_id |user_id | earnedtime  |
|--------------------------------|
|       1 |      1 | 1313548360  |
|       2 |      1 | 1313548365  |
|       3 |      1 | 1313548382  |
|       4 |      1 | 1313548410  |
+--------------------------------+

MySQL Query:

SELECT m.*, u.*
FROM users_medals u
LEFT JOIN medals m ON (u.medal_id=m.medal_id)
WHERE u.user_id IN(1)
GROUP BY m.medal_type
ORDER BY u.earnedtime

What this is intended to do is display medals users have earned (this is a plugin for a bulletin board system). It selects and displays the medals where the users medal id is equal to the medal id in the table that holds all the medals.

This works fine, however, it’s not displaying the latest medal. It’s only displaying the following medal id’s: 1, 2. It should be displaying 3 and 4.

Additional Info: I only want to display one medal from each medal type. So for example, if the user has earned two “post medals”, only the latest one earned will be displayed, along with any other medals earned.

Any help would be greatly appreciated.

  • 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-24T20:25:58+00:00Added an answer on May 24, 2026 at 8:25 pm
    SELECT m.*, u.*
      FROM users_medals u
      LEFT JOIN medals m ON u.medal_id = m.medal_id
      WHERE u.user_id IN (1)
      AND u.earnedtime = (
        SELECT MAX(users_medals.earnedtime) FROM users_medals
        LEFT JOIN medals ON users_medals.medal_id = medals.medal_id
        WHERE users_medals.user_id = u.user_id
        AND medals.medal_type = m.medal_type
      )
    

    I think it should be possible to do this as a subjoin too but the restriction of the medal type being in another table made my head ache. It would probably be easier to write if there was a view of the two tables joined together.

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

Sidebar

Related Questions

i need some help with this sql query heres my mysql table structure: DOMAINS
I need some help with a query (sorry, I'm not too good at this!)
I need some help with a MySQL query I'm working on. I have data
I need some help on a MySQL SELECT statement. Unfortunately, this statement doesn't work
I am new to all the anonymous features and need some help. I have
I have a mysql table with 3 fields: id, client, group and some data.
I need some help with a query - I'm using Firebird 2.1. I have
I just need some help to finish turning this SQL : SELECT us.name, pt.task,
I need some help from the shell-script gurus out there. I have a .txt
I need some help regarding algorithm for randomness. So Problem is. There are 50

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.