有人可以帮助我理解为什么下面的UPDATE查询产生错误吗?
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服务器版本的手册 在'UPDATE holdings h JOIN subt ON h.portfolio =附近使用的语法 subt.portfolio和h.isin'在第10行
虽然在SELECT以下可以正常工作:
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数据库
答案 0 :(得分:1)
您可以尝试在子查询上使用普通的内部联接,而不是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