mariadb 用查询结果更新表中的行

kiayqfof  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(161)

服务器:10.3.32-玛丽亚数据库
我有一个表,表中有ts(时间戳)列、一些值和一个空列。有一个查询输出ts和我想插入到空列中的值。目标是更新表中查询中有对应条目(ts匹配ts)的位置。
如何对整个表执行此操作?
两次尝试都不奏效:
1/

UPDATE avg1h
SET pwr_peak = peaks. Peak
FROM (
    SELECT ts, MAX(pwr) AS peak
    FROM (
        SELECT
            ts
            - INTERVAL EXTRACT(SECOND FROM ts) SECOND
            - INTERVAL EXTRACT(MINUTE FROM ts) MINUTE AS ts,
            CASE WHEN pwr < 0 THEN 0 ELSE pwr END as pwr
        FROM avg15m
    ) AS peak15m
    GROUP BY ts
) AS peaks
WHERE avg1h.ts = peaks.ts

2/

UPDATE avg1h
SET avg1h.pwr_peak = peaks. Peak
FROM avg1h
INNER JOIN (
    SELECT ts, MAX(pwr) AS peak
    FROM (
        SELECT
            ts
            - INTERVAL EXTRACT(SECOND FROM ts) SECOND
            - INTERVAL EXTRACT(MINUTE FROM ts) MINUTE AS ts,
            CASE WHEN pwr < 0 THEN 0 ELSE pwr END as pwr
        FROM avg15m
    ) AS peak15m
    GROUP BY ts
) AS peaks
ON avg1h.ts = peaks.ts

查询(AS峰)正确输出“ts”和“peak”列。

xe55xuns

xe55xuns1#

看来这就是答案:也许我搞混了SQL方言...

UPDATE avg1h, (
    SELECT ts, MAX(pwr) AS peak
    FROM (
        SELECT
            ts
            - INTERVAL EXTRACT(SECOND FROM ts) SECOND
            - INTERVAL EXTRACT(MINUTE FROM ts) MINUTE AS ts,
            CASE WHEN pwr < 0 THEN 0 ELSE pwr END as pwr
        FROM avg15m
    ) AS peak15m
    GROUP BY ts
) AS peaks
SET avg1h.pwr_peak = peaks.peak
WHERE avg1h.ts = peaks.ts AND avg1h.pwr_peak IS NULL

相关问题