我试图根据自身的子查询来更新表。所以我需要将子查询的结果作为UPDATE语句中的参数。
我在下面尝试过但没有成功:
UPDATE xx
INNER JOIN (
SELECT r.id as id, w.state as state
...
) yy
SET xx.state = yy.state WHERE xx.id = yy.id;
修改 完整查询:
UPDATE dpcio_request xx
INNER JOIN (
SELECT r.id as id, w.state as state
FROM dpcio_request r
JOIN dpcio_request_wf w
ON (w.dpcio_request_id = r.id)
WHERE w.id IN ( SELECT MAX(id)
FROM `dpcio_request_wf`
GROUP BY dpcio_request_id )
) r2 ON r1.id = r2.id
) yy ON yy.id = xx.id
SET xx.state = yy.state;
答案 0 :(得分:7)
如果您对子查询没有特殊计算并假设它来自不同的表,则可以直接连接表,
UPDATE table1 a
INNER JOIN table2 b
ON a.id = b.id
SET a.colName = b.value
更新1
这是查询的简化版本,
UPDATE dpcio_request a
INNER JOIN dpcio_request_wf b
ON a.id = b.dpcio_request_wf
INNER JOIN
(
SELECT dpcio_request_id, MAX(id) max_ID
FROM dpcio_request_wf
GROUP BY dpcio_request_id
) c ON b.id = c.max_ID AND
b.dpcio_request_id = c.dpcio_request_id
SET a.state = b.state