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

Aya*_*hah 0 php mysql

我想在mysql中获取最后一行特定的id.假设我有一个表用户产品

    userProducts

    id | user_id  | products 
    1 | 12| Mouse
    2 | 12| Keyboard
    3 | 12| Laptop
    4 | 12| Headphone
    5 | 12| Webcame
Run Code Online (Sandbox Code Playgroud)

我想获得最后一行Webcame user_id=12.请指导我如何为此自定义此查询.

select * from userProducts where user_id = 12
Run Code Online (Sandbox Code Playgroud)

Luk*_*zda 6

您需要按id排序并限制结果集:

SELECT * 
FROM userProducts
WHERE user_id = 12
ORDER BY id  DESC
LIMIT 1
Run Code Online (Sandbox Code Playgroud)