MySQL 查询适用于 SELECT 但不适用于 UPDATE 语句

MySQL query works for SELECT but not with UPDATE statement

我 运行 遇到了一个问题,我的第一个查询 ( 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, 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
)

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 ...

没有更新选项。