简体   繁体   English

根据其他表列更新表列

[英]Updating table column based on other table column

I want to update table cases_proceedings but it will check some criteria for this in the other table cases_attach_files ie 我想更新表cases_proceedings,但是它将在其他表cases_attach_files中检查一些标准,即

SELECT cp.sno FROM cases_proceedings cp, cases_attach_files cf 
WHERE cp.`case_sno` = cf.`case_sno` AND cf.page_lbl_sno = 10 AND 
cp.`is_decided` = 0

But when i want to update the first table value and search the sno of the first table by using the IN operation with subquery like this: 但是,当我想更新第一个表的值并通过对子查询使用IN操作来搜索第一个表的sno时 ,如下所示:

UPDATE cases_proceedings 
     SET `is_decided` = 1 
WHERE sno IN(SELECT cp.sno FROM cases_proceedings cp, cases_attach_files cf 
WHERE cp.`case_sno` = cf.`case_sno` AND cf.page_lbl_sno = 10 AND 
cp.`is_decided` = 0);

It shows an error: 它显示一个错误:

Error Code: 1093
You can't specify target table 'cases_proceedings' for update in FROM clause

i don't know how to fix this? 我不知道该如何解决?

One workaround here is to wrap the subquery in another subquery: 一种解决方法是将子查询包装在另一个子查询中:

UPDATE cases_proceedings 
SET is_decided = 1 
WHERE sno IN
(
    SELECT sno
    FROM
    (
        SELECT cp.sno
        FROM cases_proceedings cp
        INNER JOIN cases_attach_files cf 
            ON cp.case_sno = cf.case_sno AND
               cf.page_lbl_sno = 10 AND
               cp.is_decided = 0
    ) t
);

The extra subquery works around the error because it forced MySQL to materialize the subquery containing the joins. 额外的子查询可解决该错误,因为它迫使MySQL实现包含联接的子查询。

According to documentation , In MySQL, you can't modify the same table which you use in the SELECT part. 根据文档 ,在MySQL中,您不能修改在SELECT部分​​中使用的同一表。

You can either wrap it in subquery or joining both tables as shown below: 您可以将其包装在子查询中,也可以将两个表连接起来,如下所示:

UPDATE  cases_proceedings cp
        INNER JOIN cases_attach_files cf
            ON cp.case_sno = cf.case_sno
                AND cf.page_lbl_sno = 10 
                AND cp.is_decided = 0
SET     cp.is_decided = 1 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM