我有兩個表:如何使用生成多行的select語句更新表?
[Table container]
id int
<some more fields>
latest_measurement int
[Table measurement]
id int
container_id int
unixtime bigint
現在我想根據從測量表測量的最新更新傳感器表latest_measurement
列。我已經準備了子查詢每sensor_id
返回最新的測量和這個作品:
SELECT m.fill_level
from measurement m
inner join (
select m.container_id, MAX(m.unixtime) as maxdate from measurement m GROUP BY container_id
) m2
on m2.container_id = m.container_id
where m.unixtime = m2.maxdate
但是,如果我再在我的更新語句中使用此查詢,如下所示時,出現異常:
UPDATE container
SET latest_fill_level = (
SELECT m.fill_level
from measurement m
inner join (
select m.container_id, MAX(m.unixtime) as maxdate from measurement m GROUP BY container_id
) m2
on m2.container_id = m.container_id
where m.unixtime = m2.maxdate
and container.id = m.container_id)
最後,這裏的例外:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
如何編寫可以從SELECT語句塔更新各自的價值多行的更新語句t產生多個值?
你能提供多少記錄呢你內心的查詢結果? ('select m.fill_level from ...') –