I am looking to update rows where the date column is NULL by averaging the dates of the nearest id before and nearest id after that row.
In the data below, it would update the row with ID 26 to be 2011-10-19
What MySQL statement would accomplish this?
Additional Notes:
The data looks something like this:
id date title
--------------------------------------------
12 2011-09-01 Example One
23 2011-10-02 Example Two
26 NULL Example Three
27 2011-11-05 Example Four
29 2012-01-05 Example Five
37 NULL Example Six
38 2012-02-03 Example Seven
--------------------------------------------
I would prefer not to use a procedure.
So far I’ve got…
UPDATE `table`
SET `date`=
(AVG(
(SELECT `date` FROM `table` WHERE `id`< ID_OF_PARENT_QUERY AND `date` IS NOT NULL LIMIT 1),
(SELECT `date` FROM `table` WHERE `id`> ID_OF_PARENT_QUERY AND `date` IS NOT NULL LIMIT 1)
))
WHERE `date` IS NULL
1 Answer