Reputation: 51
How we can delete multiple rows having columns from a db? Suppose db has following data (id, list_name, user) and list_name has values as:
Owner-aaa
coowner-aaa
owner-aaa
subowner-aaa
How we can delete the rows having "Owner-aaa" and "owner-aaa" which are duplicates?
Can we add something in this query here:
delete from <table_name> where list_name = 'owner-aaa'
But it deletes only lower case list, I want something general which checks duplicates in small and caps and delete both of them?
Thanks in advance Amy
Upvotes: 0
Views: 3515
Reputation: 2418
DELETE a
FROM list a
INNER JOIN list b ON LOWER(a.list_name)=LOWER(b.list_name)
WHERE a.id <> b.id
Upvotes: 0
Reputation: 5722
I'm not entirely sure from your question whether you want to delete all rows where duplicates occur, or leave one, and remove only the true duplicates. So here's a shot at each:
To remove only the true duplicates:
DELETE FROM MyTable WHERE id IN
(
SELECT T1.id
FROM MyTable T1
INNER JOIN MyTable T2
ON UPPER(T1.list_name) = UPPER(T2.list_name)
AND T2.id <> T1.id
AND (T1.id <> (SELECT MAX(id) FROM MyTable WHERE UPPER(list_name) = UPPER(T1.list_name))
) DUPS
This presumes that the id field is unique to each record
To remove all records where there are duplicates, remove the two "AND" clauses in the subquery.
Upvotes: 0
Reputation: 2036
Meaby you can use LOWER/UPPER sql functions.
But are you sure your model is correct? It seem realy weird to have a name list like that. That should be another table NAMES with ID and NAME field. It's a 1-N relation.
Upvotes: 0
Reputation: 34527
DELETE FROM mytable WHERE LOWER(listname) IN
(SELECT LOWER(listname) FROM mytable
GROUP BY LOWER(listname)
HAVING COUNT(*) > 1)
Upvotes: 1