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.
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.