This one has me stumpped.
I have 2 tables as so:
METERS
id | startTime
READINGS
id | meter_id | readingTime
What I want to do is update the meters.startTime
to the lowest matching readings.readingTime
in 1 sql query.
How do I do this?
Like this:
UPDATE Meters m
INNER JOIN
(
SELECT meter_id, MIN(reading_time) lowesttime
FROM readings
GROUP BY meter_id
) r ON m.id = r.meter_id
SET m.starttime = r.lowesttime;
UPDATE METERS m SET startTime = (SELECT MIN(r.readingTime)
FROM READINGS r
WHERE r.meter_id = m.id)
WHERE m.id = your_id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With