dqqvravff05370501 2015-10-19 04:28
浏览 66
已采纳

如何在mysql中获取特定id的最后一行? [重复]

This question already has an answer here:

I'm trying to get last row of specific id in mysql. Let suppose i have a table user products

    userProducts

    id | user_id  | products 
    1 | 12| Mouse
    2 | 12| Keyboard
    3 | 12| Laptop
    4 | 12| Headphone
    5 | 12| Webcame

I want to get last row Webcame of user_id=12. Please guide me how can i customize this query for that.

select * from userProducts where user_id = 12
</div>
  • 写回答

2条回答 默认 最新

  • duanli0687 2015-10-19 04:28
    关注

    You need to sort by id and limit resultset:

    SELECT * 
    FROM userProducts
    WHERE user_id = 12
    ORDER BY id  DESC
    LIMIT 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?