MySQL query to update records with incremented date
I am trying to
get the latest date in a database, and
based on that date update every record that has a NULL date, increasing
the date by 1 day.
I can get the latest date by using the Latest Date query below. I need to
do this first because the dates in the table are not in order. If need be,
I can run this query, manually write it down, then run the UPDATE query
based on this date. I would prefer to run everything without the manual
process.
The last query I have at the bottom of the question is my test query for
trying to update the dates, however I had no luck getting it to work.
Table (dates are not in order)
id date
----- ----------
10500 2013-08-18
10501 2013-08-16
10502 2013-08-17
10503 2013-08-19
10504 NULL
10505 NULL
...
11800 NULL
11801 NULL
Selecting the latest date (starting point for UPDATE)
SELECT date
FROM my_table
ORDER BY date DESC
LIMIT 1
Updating NULL dates (doesn't work)
UPDATE my_table
SET date = DATE_ADD((SELECT date FROM my_table ORDER BY date DESC LIMIT
1), INTERVAL 1 DAY)
WHERE date IS NULL
ORDER BY id ASC
How can I accomplish this? Or is this not possible?
No comments:
Post a Comment