4

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?

2 Answers 2

14

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; 
Sign up to request clarification or add additional context in comments.

Comments

2
UPDATE METERS m SET startTime = (SELECT MIN(r.readingTime) FROM READINGS r WHERE r.meter_id = m.id) WHERE m.id = your_id 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.