MySQL - GROUP BY with ORDER DESC not working

前端 未结 1 332
猫巷女王i
猫巷女王i 2021-01-29 12:29

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

相关标签:
1条回答
  • 2021-01-29 13:11

    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

    0 讨论(0)
提交回复
热议问题