CREATE TABLE mytable
(id SERIAL NOT NULL PRIMARY KEY
, ipaddress varchar
);
INSERT INTO mytable(id, ipaddress) VALUES
(1, '192.168.xxx.xxx')
,(2, '192.168.xxx.xxx ') --<< note trailing whitespace
,(3, '111.118.xxx.xxx')
,(4, '111.118.xxx.xxx')
;
SELECT * FROM mytable;
DELETE FROM mytable mt
WHERE EXISTS (
SELECT * FROM mytable ex
WHERE ex.ipaddress = mt.ipaddress
AND ex.id < mt.id
)
;
SELECT * FROM mytable;
DELETE FROM mytable mt
WHERE EXISTS (
SELECT * FROM mytable ex
WHERE TRIM(ex.ipaddress) = TRIM(mt.ipaddress)
AND ex.id < mt.id
)
;
SELECT * FROM mytable;
OUTPUT:
CREATE TABLE
INSERT 0 4
id | ipaddress
----+------------------
1 | 192.168.xxx.xxx
2 | 192.168.xxx.xxx
3 | 111.118.xxx.xxx
4 | 111.118.xxx.xxx
(4 rows)
DELETE 1
id | ipaddress
----+------------------
1 | 192.168.xxx.xxx
2 | 192.168.xxx.xxx
3 | 111.118.xxx.xxx
(3 rows)
DELETE 1
id | ipaddress
----+-----------------
1 | 192.168.xxx.xxx
3 | 111.118.xxx.xxx
(2 rows)
UPDATE: added testdata and changed one record to have trailing whitspace.
NOTE: the names of the stringfunctions may vary between DMBS implementations. The TRIM() function works for postgres, maybe mysql has another name for the same thing.
UPDATE2: since mysql appears to not allow selfjoins in delete statements a workaround would be to use an auxillary table with the ids of the records you (don't) want to keep.
(the solution by @ahose_with_no_name is shorter, but this one tries to keep close to the plain-vanilla SQL) :
CREATE table without_dups(id INTEGER NOT NULL);
INSERT INTO without_dups(id)
SELECT id
FROM mytable mt
WHERE NOT EXISTS (
SELECT * FROM mytable ex
WHERE ex.ipaddress = mt.ipaddress
AND ex.id < mt.id
)
;
DELETE FROM mytable mt
WHERE NOT EXISTS (
SELECT * FROM without_dups nx
WHERE nx.id = mt.id
)
;
DROP TABLE without_dups;
SELECT * FROM mytable;