4

这个让我难住了。

我有2张桌子:

METERS
id | startTime

READINGS
id | meter_id | readingTime

我想要做的是将1 sql 查询中meters.startTime的最低匹配更新。readings.readingTime

我该怎么做呢?

4

2 回答 2

14

像这样:

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;
于 2012-12-10T10:36:06.383 回答
2
UPDATE METERS m SET startTime = (SELECT MIN(r.readingTime)
    FROM READINGS r
    WHERE r.meter_id = m.id)
WHERE m.id = your_id
于 2012-12-10T10:36:45.193 回答