在表格中我有2列,IP地址和日期。有ip地址重复值。但日期是独一无二的。我需要删除所有重复的IP,留下最新的IP
答案 0 :(得分:1)
如果您使用DELETE JOIN
:
mysql
delete t1
from yourtable t1
join yourtable t2
on t1.ip = t1.ip
and t1.`date` < t2.`date`
答案 1 :(得分:1)
此查询显示了一种为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>)
答案 2 :(得分:0)
您想要删除存在更新条目的所有记录(即具有相同IP地址和更高日期的条目):
$delegate.someproperty