1

我正在尝试根据自身的子查询更新表。所以我需要子查询的结果作为 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;
4

1 回答 1

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
于 2013-02-09T06:35:01.507 回答