我想使用相关子查询更新表中的多个列。更新单个列非常简单:
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
表)
geometry
)temperature
表格中存在温度temperature
表格行
route
表