Sql 删除保留最新记录的重复记录
在一个表中,我有两列,IPaddress和date。存在ip地址重复值。但日期是独一无二的。我需要删除所有重复的IP,保留最新的IP假设您没有空值并且分配了主键,请尝试此查询Sql 删除保留最新记录的重复记录,sql,oracle,sql-delete,Sql,Oracle,Sql Delete,在一个表中,我有两列,IPaddress和date。存在ip地址重复值。但日期是独一无二的。我需要删除所有重复的IP,保留最新的IP假设您没有空值并且分配了主键,请尝试此查询 DELETE FROM MyTable LEFT OUTER JOIN ( SELECT MIN(RowId) as RowId, ipaddress, date FROM MyTable GROUP BY ipaddress, date ) as KeepRows ON MyTable.Ro
DELETE FROM MyTable
LEFT OUTER JOIN (
SELECT MIN(RowId) as RowId, ipaddress, date
FROM MyTable
GROUP BY ipaddress, date
) as KeepRows ON
MyTable.RowId = KeepRows.RowId
WHERE
KeepRows.RowId IS NULL
根据您的要求进行编辑
首先,需要将标识设置到表中,然后使用此用户设置标识
ALTER TABLE [tablename] ADD Id INT IDENTITY(1,1)
一旦设置了标识,请执行以下查询以删除重复的IP地址
DELETE FROM dbo.DataTime WHERE ColumnID NOT IN (SELECT MIN(ColumnID) _
FROM dbo.DataTime group by (ipaddress))
如果使用mysql,您可以尝试
DELETE JOIN
:
delete t1
from yourtable t1
join yourtable t2
on t1.ip = t1.ip
and t1.`date` < t2.`date`
删除t1
从您的表t1
加入您的表t2
在t1.ip=t1.ip上
和t1.`date`
试试这个
您希望删除所有存在较新条目(即具有相同IP地址和较高日期的条目)的记录:
此查询显示了一种为ip选择除最新行以外的所有行的方法:
WITH
--this bit of code just creates some data we can play with because I
--don't have your table structure in my database
ip_data (ip, creation_date)
AS
(SELECT '1.2.3.4',sysdate-1 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-2 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-3 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-4 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-5 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-1 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-2 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-3 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-4 FROM dual
)
--this query takes our data with row numbers and excludes any with row number
--1 (that is, the most recent row for each ip)
SELECT
ip
,creation_date
FROM
--this query assigns a row number to each row. The latest row for an ip
--get row number 1. Numbering restarts for each ip (PARTITION BY ip)
(SELECT
ip
,creation_date
,ROW_NUMBER() OVER (PARTITION BY ip ORDER BY creation_date DESC) rn
FROM
ip_data
)
WHERE rn > 1
;
您需要为您的表结构重新编写此注释,但希望注释有意义。一旦您了解了它的工作原理,您就可以将其应用到以下内容中:
DELETE FROM <your table>
WHERE (ip, creation_date) IN
(<select statement similar to the above>)
从中删除
其中(ip、创建日期)
()
尝试使用distinct您想只获取唯一的ip地址,还是要删除它?首先确定您使用的是哪种RDBMS。然后看我在使用oracle sql devoolper不要按注释编辑答案,而是将注释添加到注释框中。他没有两个表。@user7417866这是自联接。@user7417866、t1和t2是所谓的表别名,这里引用了同一基表的两个实例。您好,谢谢您的快速响应。实际上没有行ID。这是一个带有列的表-IP地址、区号、添加的日期我想删除重复的IP地址,只保留最新的日期谢谢您的快速响应请解释结构中的aa和bb值。感谢you@Ashfen99它们只是别名
WITH
--this bit of code just creates some data we can play with because I
--don't have your table structure in my database
ip_data (ip, creation_date)
AS
(SELECT '1.2.3.4',sysdate-1 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-2 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-3 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-4 FROM dual UNION ALL
SELECT '1.2.3.4',sysdate-5 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-1 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-2 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-3 FROM dual UNION ALL
SELECT '1.2.3.9',sysdate-4 FROM dual
)
--this query takes our data with row numbers and excludes any with row number
--1 (that is, the most recent row for each ip)
SELECT
ip
,creation_date
FROM
--this query assigns a row number to each row. The latest row for an ip
--get row number 1. Numbering restarts for each ip (PARTITION BY ip)
(SELECT
ip
,creation_date
,ROW_NUMBER() OVER (PARTITION BY ip ORDER BY creation_date DESC) rn
FROM
ip_data
)
WHERE rn > 1
;
DELETE FROM <your table>
WHERE (ip, creation_date) IN
(<select statement similar to the above>)