我想使用相关子查询更新表中的多个列。更新单个列很简单:
UPDATE route
SET temperature = (SELECT amb_temp.temperature
FROM amb_temp.temperature
WHERE amb_temp.location = route.location)
但是,我想更新路由表的几列。由于子查询实际上要复杂得多(使用 SpatiaLite 函数连接嵌套子查询),我想避免像这样重复它:
UPDATE route
SET
temperature = (SELECT amb_temp.temperature
FROM amb_temp.temperature
WHERE amb_temp.location = route.location),
error = (SELECT amb_temp.error
FROM amb_temp.temperature
WHERE amb_temp.location = route.location),
理想情况下,SQLite 会让我做这样的事情:
UPDATE route
SET (temperature, error) = (SELECT amb_temp.temperature, amb_temp.error
FROM amb_temp.temperature
WHERE amb_temp.location = route.location)
唉,这是不可能的。可以用其他方法解决吗?
这是我到目前为止一直在考虑的事情:
为了完整起见,这是我正在处理的实际 SQL 查询:
UPDATE route SET (temperature, time_distance) = ( -- (C)
SELECT -- (B)
temperature.Temp,
MIN(ABS(julianday(temperature.Date_HrMn)
- julianday(route.date_time))) AS datetime_dist
FROM temperature
JOIN (
SELECT -- (A)
*, Distance(stations.geometry,route.geometry) AS distance
FROM stations
WHERE EXISTS (
SELECT 1
FROM temperature
WHERE stations.USAF = temperature.USAF
AND stations.WBAN_ID = temperature.NCDC
LIMIT 1
)
GROUP BY stations.geometry
ORDER BY distance
LIMIT 1
) tmp
ON tmp.USAF = temperature.USAF
AND tmp.WBAN_ID = temperature.NCDC
)
此查询的高级描述:
geometry
表中的 date_time
(= 经度和纬度)和 route
,stations
表,由 USAF 和 NCDC/WBAN_ID 列唯一标识)
geometry
)temperature
表中列出了哪些温度temperature
表格行
route
表中UPDATE route
SET
temperature = q.temperature,
error = q.error
FROM ( SELECT * FROM amb_temp ) q
WHERE q.location = route.location
?