-2

Hi I am having a problem with following query.

SELECT id, user_id, cloth_id FROM `items` GROUP BY user_id ORDER BY id desc LIMIT 3 

I want the latest records with group by but somehow its showing old records.

I have also gone through MySQL - Group by with Order by DESC but not working as expected.

Community
  • 1
  • 1

1 Answers1

1

Try this:

SELECT i.id, i.user_id, i.cloth_id FROM
(
    SELECT max(id) as id, user_id FROM `items` GROUP BY user_id
) temp
LEFT JOIN `items` i on i.user_id = temp.user_id AND i.id = temp.id

in temp you get each user with the latest id.
in i you get the cloth_id for that combination

Alex Tartan
  • 6,736
  • 10
  • 34
  • 45