其他分享
首页 > 其他分享> > 同表不支持update子查询的结果

同表不支持update子查询的结果

作者:互联网

[root@db01 /data/backup]# mysql.dba zhouwanchun

[root@localhost:mysql3306.sock] [zhouwanchun]> UPDATE work_flow_event
SET process_status = '3', process_message = '审批拒绝'
WHERE flow_no IN (
        SELECT flow_no
        FROM work_flow_event
        WHERE flow_no IN (
                SELECT flow_no
                FROM work_flow
                WHERE flow_data -> '$.customerNumber' = '101379'
                    AND flow_key LIKE 'price%'
                    AND flow_data -> '$.endTime' < '2021-05-11'
                    AND curr_node = '02'
            )
            AND node_code = '02'
    );
ERROR 1093 (HY000): You can't specify target table 'work_flow_event' for update in FROM clause

 

[root@localhost:mysql3306.sock] [zhouwanchun]> UPDATE work_flow_event
SET process_status = '3', process_message = '审批拒绝'
WHERE flow_no IN (select * from (
        SELECT flow_no
        FROM work_flow_event as a
        WHERE a.flow_no IN (
                SELECT b.flow_no
                FROM work_flow as b
                WHERE b.flow_data -> '$.customerNumber' = '101379'
                    AND b.flow_key LIKE 'price%'
                    AND b.flow_data -> '$.endTime' < '2021-05-11'
                    AND b.curr_node = '02'
            )
            AND a.node_code = '02'
            ));
ERROR 1248 (42000): Every derived table must have its own alias

 

[root@localhost:mysql.sock] [zhouwanchun]> UPDATE work_flow_event
SET process_status = '3', process_message = '审批拒绝'
WHERE flow_no IN (select * from (
        SELECT flow_no
        FROM work_flow_event as a
        WHERE a.flow_no IN (
                SELECT b.flow_no
                FROM work_flow as b
                WHERE b.flow_data -> '$.customerNumber' = '101379'
                    AND b.flow_key LIKE 'price%'
                    AND b.flow_data -> '$.endTime' < '2021-05-11'
                    AND b.curr_node = '02'
            )
            AND a.node_code = '02'
            ) as b);
Query OK, 0 rows affected (0.72 sec)
Rows matched: 3644  Changed: 0  Warnings: 0

-- 总结

派生表要加表别名。 原始表不能出现在 where 后面第一层的子查询当中。 在同一语句中,不能先 select 出同一表中的某些值,再 update 这个表。 同表不支持 update 子查询的结果,将子查询结果,再 select 一次,这样就规避了错误。

标签:no,data,work,flow,update,同表,查询,WHERE,event
来源: https://www.cnblogs.com/zhouwanchun/p/16554168.html