如何解决MySQL查询适用于SELECT,但不适用于UPDATE语句
我遇到一个问题,我的第一个查询(SELECT)工作正常,但是当我尝试执行我的第二个查询(UPDATE)时,出现错误。有人可以帮我知道我在做什么错吗?
QUERY 1:
WITH res AS (
SELECT str.request_id as requestID,str.type as incorrectType,TxType.utType as correctType
FROM x str
JOIN (
SELECT tr.request_id,ut.type AS utType
FROM x tr
JOIN y tc ON tr.request_id = tc.request_id
JOIN z ut ON tc.transaction_id = ut.id
) AS TxType ON str.request_id = TxType.request_id
WHERE str.type != TxType.utType
AND str.application = 'sample' LIMIT 1
)
SELECT * FROM x tr_req
JOIN res AS re ON re.requestID = tr_req.request_id
WHERE tr_req.type != re.correctType;
RESULT : SUCCESS
QUERY : 2
WITH res AS (
SELECT str.request_id as requestID,ut.type AS utType
FROM x tr
JOIN y tc ON tr.request_id = tc.request_id
JOIN z ut ON tc.transaction_id = ut.id
) AS TxType ON str.request_id = TxType.request_id
WHERE str.type != TxType.utType
AND str.application = 'sample' LIMIT 1
)
UPDATE x tr_req
JOIN res AS re ON re.requestID = tr_req.request_id
SET tr_req.type = re.correctType
WHERE tr_req.type != re.correctType;
RESULT : You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UPDATE x tr_req
解决方法
您正在使用MariaDB。 Here is the documentation of WITH。
如您在语法中所见,它仅适用于SELECT:
WITH [RECURSIVE] table_reference [(columns_list)] AS (
SELECT ...
)
[CYCLE cycle_column_list RESTRICT]
SELECT ...
没有UPDATE选项。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。