I need to get the last inserted product by a user. This is what I’ve came up with
$query = 'SELECT id
FROM products
WHERE user_id = ?
ORDER BY id DESC
LIMIT 1';
It should work because the id is auto increment. To me though, it feels hacky. Is there a better way to do this?
I can’t use mysql_last_insert_id() or anything like that because I need the last product by a specific user.
Use:
MAX is an aggregate function, returning the maximum value of the column specified. Because the query is only returning one column – the one the aggregate function is being used on – the
GROUP BYclause doesn’t need to be specified. But there’s no harm in using: