DELETE 的目标表结果不可更新。怎么解决?

问题描述 投票:0回答:1

我尝试使用 CTE 和子查询,它们都给出与“删除的目标表结果不可更新”相同的错误。我尝试谷歌但没有找到有用的资源。

customers table

CTE代码

with Result as
(
    select 
        *,
        row_number() over(partition by id order by id) as RowNo 
    from 
        customers
)
delete from Result 
where RowNo > 1;

子查询代码

delete Result 
from (select *, row_number() over(partition by id order by id) as RowNo 
      from customers) Result 
where Result.RowNo > 1; 
mysql subquery common-table-expression
1个回答
0
投票

图案:

DELETE t1
FROM table t1
JOIN table t2 ON t1.ident_column = t2.ident_column
             AND t1.order_column < t2.order_column

DELETE
FROM table t1
WHERE EXISTS ( SELECT NULL 
               FROM table t2
               WHERE t1.ident_column = t2.ident_column
                 AND t1.order_column < t2.order_column )

此模式通过

ident_column
仅保存组中
order_column
中具有最大值的行来删除重复的行。

© www.soinside.com 2019 - 2024. All rights reserved.