where不起作用的多表更新

ssm49v7z  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(429)

有人能帮我理解为什么下面的更新查询会产生错误吗?

WITH subt AS (
                SELECT t.portfolio, s.isin, t.quantity, t.date
                    FROM transactions t
                    JOIN stocks s
                        ON t.stock = s.name
                    ORDER BY t.id DESC
                    LIMIT 1
                )

UPDATE holdings h
    JOIN subt
        ON h.portfolio = subt.portfolio
            AND h.isin = subt.isin
    SET h.end_date = DATE_SUB(subt.date, INTERVAL 1 DAY)
    WHERE h.end_date is NULL

我得到的错误是:
sql错误(1064):您的sql语法有错误;检查与您的mariadb服务器版本对应的手册,了解第10行“update holdings h join subt on h.portfolio=subt.portfolio and h.isin”附近使用的正确语法
在下面,选择“工作正常”:

WITH subt AS (
                SELECT t.portfolio, s.isin, t.quantity, t.date
                    FROM transactions t
                    JOIN stocks s
                        ON t.stock = s.name
                    ORDER BY t.id DESC
                    LIMIT 1
                )

SELECT h.*
FROM holdings h
    JOIN subt
        ON h.portfolio = subt.portfolio
            AND h.isin = subt.isin
    WHERE h.end_date is NULL

我正在通过heidisql使用mariadb 10数据库

jmo0nnb3

jmo0nnb31#

您可以尝试对子查询使用普通的内部联接,而不是with结果

UPDATE holdings h
JOIN 
  (
            SELECT t.portfolio, s.isin, t.quantity, t.date
                FROM transactions t
                JOIN stocks s
                    ON t.stock = s.name
                ORDER BY t.id DESC
                LIMIT 1
            )  subt
    ON h.portfolio = subt.portfolio
        AND h.isin = subt.isin
SET h.end_date = DATE_SUB(subt.date, INTERVAL 1 DAY)
WHERE h.end_date is NULL

相关问题