在删除 MySQL 中的重复项时,DELETE 的目标 table 不是 updatable
Target table of DELETE is not updatable while deleting duplicates in MySQL
我正在使用 windows 函数 Row_Number()
编写此查询,它将找出重复项,我正尝试 delete
这些重复项。
为此,我编写了 CTE
并包含 window 函数并尝试删除重复行。但是,我收到错误消息说删除不可更新。
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 rows
是的,CTE 对很多事情都非常有用,但对您的目的却不是。
改用 INNER JOIN
.
CREATE TABLE housingdata (UniqueID int,
ParcelID int
, PropertyAddress varchar(50)
,
SalePrice DECIMAL(10,2)
,saledate Date
,LegalReference int)
INSERT INTO housingdata VALUES (1,1,'test',1.1, NOW(), 1),(2,1,'test',1.1, NOW(), 1)
delete hd
FROM housingdata hd INNER JOIN
(
select UniqueID ,row_number() over (partition by ParcelID, PropertyAddress,
SalePrice,saledate,LegalReference order by UniqueID) as rownum
from housingdata) t1 ON hd.UniqueID = t1.UniqueID
WHERE t1.rownum>1;
SELECT * FROM housingdata
UniqueID | ParcelID | PropertyAddress | SalePrice | saledate | LegalReference
-------: | -------: | :-------------- | --------: | :--------- | -------------:
1 | 1 | test | 1.10 | 2022-02-25 | 1
db<>fiddle here
更新
您也可以使用已加入的 CTE table
with rownumcte as (
select UniqueID ,row_number() over (partition by ParcelID, PropertyAddress,
SalePrice,saledate,LegalReference order by UniqueID) as rownum
from housingdata)
delete hd
from housingdata hd INNER JOIN rownumcte r ON hd.UniqueID = r.UniqueID
where rownum>1;
SELECT * FROM housingdata
UniqueID | ParcelID | PropertyAddress | SalePrice | saledate | LegalReference
-------: | -------: | :-------------- | --------: | :--------- | -------------:
1 | 1 | test | 1.10 | 2022-02-25 | 1
db<>fiddle here
我正在使用 windows 函数 Row_Number()
编写此查询,它将找出重复项,我正尝试 delete
这些重复项。
为此,我编写了 CTE
并包含 window 函数并尝试删除重复行。但是,我收到错误消息说删除不可更新。
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 rows
是的,CTE 对很多事情都非常有用,但对您的目的却不是。
改用 INNER JOIN
.
CREATE TABLE housingdata (UniqueID int, ParcelID int , PropertyAddress varchar(50) , SalePrice DECIMAL(10,2) ,saledate Date ,LegalReference int)
INSERT INTO housingdata VALUES (1,1,'test',1.1, NOW(), 1),(2,1,'test',1.1, NOW(), 1)
delete hd FROM housingdata hd INNER JOIN ( select UniqueID ,row_number() over (partition by ParcelID, PropertyAddress, SalePrice,saledate,LegalReference order by UniqueID) as rownum from housingdata) t1 ON hd.UniqueID = t1.UniqueID WHERE t1.rownum>1;
SELECT * FROM housingdata
UniqueID | ParcelID | PropertyAddress | SalePrice | saledate | LegalReference -------: | -------: | :-------------- | --------: | :--------- | -------------: 1 | 1 | test | 1.10 | 2022-02-25 | 1
db<>fiddle here
更新
您也可以使用已加入的 CTE table
with rownumcte as ( select UniqueID ,row_number() over (partition by ParcelID, PropertyAddress, SalePrice,saledate,LegalReference order by UniqueID) as rownum from housingdata) delete hd from housingdata hd INNER JOIN rownumcte r ON hd.UniqueID = r.UniqueID where rownum>1;
SELECT * FROM housingdata
UniqueID | ParcelID | PropertyAddress | SalePrice | saledate | LegalReference -------: | -------: | :-------------- | --------: | :--------- | -------------: 1 | 1 | test | 1.10 | 2022-02-25 | 1
db<>fiddle here