【问题标题】:The target table Result of the DELETE is not updatable. How to fix it?DELETE 的目标表 Result 不可更新。如何解决?
【发布时间】:2021-08-13 10:48:05
【问题描述】:

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

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】:

    图案:

    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 中具有最大值的行保存为组。

    【讨论】:

    • 我正在尝试这样 DELETE t1 FROM customers t1 JOIN customers t2 ON t1.id = t2.id AND t1.id &lt; t2.id; 查询正在执行,0 行受影响
    • @RamE 请先考虑。 t1.id 不能同时等于和小于 t2.id
    • 我已经在问题中更新了我的表格,您可以看看吗?
    • @RamE 示例数据必须以文本形式提供 CREATE TABLE + INSERT INTO 并附有所需的最终数据状态。
    • @RamE 在您的屏幕截图中,重复的行是完全重复的。如果这是您的表的完整状态,那么您无法在单个查询表单中执行所需的操作。
    猜你喜欢
    • 2013-04-06
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多