Jucain Jucain - 1 month ago 5
MySQL Question

SQL Update Table Where date = MIN(date)

I got the following code:

Update `Table` set amount='1003' WHERE date = (SELECT MIN(date)) AND `id` = 736


Something is wrong with my first Where rule
date = (SELECT MIN(date))
but i dont know what.

Answer

You can update it from a join:

Update `Table` a
INNER JOIN (
    SELECT `id`, min(exp_date) AS exp_date from `Table` WHERE `id`= 736
) AS b ON (a.id=b.id AND a.exp_date=b.exp_date)
set amount='1003'
WHERE a.id = 736 AND a.exp_date=b.exp_date;
Comments