在 MySQL 中删除重复项时,DELETE 的目标表不可更新
我正在使用 Windows 函数 Row_Number()
编写此查询,它将找出重复项,并且我正在尝试删除
这些重复项。
为此,我编写了 CTE 并包含了窗口函数并尝试删除重复行。但是,我收到错误消息,说删除不可更新。
select * from housingdata;
.
.
.
with rownumcte as (
select * ,row_number() over (partition by ParcelID, PropertyAddress,
SalePrice,saledate,LegalReference order by UniqueID) as rownum
from housingdata)
delete
from rownumcte
where rownum>1;
如果我使用 select
而不是 delete
我将得到以下包含重复项的输出,即 104 行
I am writing this query using windows function Row_Number()
which will find out duplicates and i am trying to delete
those duplicates.
To do this i have written CTE
and included window function it and attempting to delete duplicate row. However, i am getting error saying delete is not updatable.
select * from housingdata;
.
.
.
with rownumcte as (
select * ,row_number() over (partition by ParcelID, PropertyAddress,
SalePrice,saledate,LegalReference order by UniqueID) as rownum
from housingdata)
delete
from rownumcte
where rownum>1;
if i use select
instead of delete
i am getting following output containing duplicates which is 104 rows
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,CTE 对于很多事情来说都非常好,但对于您的目的而言却并非如此。
请改用
INNER JOIN
。db<>fiddle此处
更新
您也可以使用 CTE 作为连接表
db<>fiddle 此处
Yes CTE are for many things very good, but for your purpose not.
Use instead a
INNER JOIN
.db<>fiddle here
UPDATE
You could have used also the CTE as joined table
db<>fiddle here