lundi 11 juillet 2016

MySQL nested update

I have got a database with many wrong entries in the field timestamp (set at 0000-00-00 00:00:00, what I want to do is to update this field with the last valid timestamp. I've tried with this query, unfortunately with no success (the syntax is not accepted):

UPDATE `scontrini` s1
SET s1.`timestamp` = 
    (SELECT TOP 1 s2.`timestamp`
        FROM `scontrini` s2
        WHERE s2.`timestamp` <> '0000-00-00 00:00:00' AND s2.`id` < s1.`id`
        ORDER BY s2.`id` DESC)
WHERE `scontrini`.`timestamp`= '0000-00-00 00:00:00';

Some solutions suggest using an inner join, but I am not testing the equality of some value.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire