0

I have a sql database named "data" and a table "disk", where there are 5 columns

CREATE TABLE `disk` (
  `id` int(11) NOT NULL,
  `title` text COLLATE utf8_unicode_ci NOT NULL,
  `link` text COLLATE utf8_unicode_ci NOT NULL,
  `mag` text COLLATE utf8_unicode_ci NOT NULL,
  `size` varchar(10) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

the "mag" column has a some of the duplicates.

and I want to delete the complete row where mag column is same.

note: let's say mag column has 1,2,3,4,4,5.... I want to delete a single 4 from it which is duplicate. means I don't want to completely delete both the 4. one "4" must be kept.

What should the query for it look like?

3
  • 3
    Which id = 4 row to keep? Commented Apr 20, 2018 at 9:26
  • Consider to include sql for DDL and sample data in your questions, so it will help other members give you answer faster and more precise Commented Apr 20, 2018 at 9:35
  • both are same doesn't matter @jarlh Commented Apr 20, 2018 at 9:38

5 Answers 5

6

try this below to delete duplicate with same d column and keep one row with lowest id value: :

DELETE d1 
FROM  disk d1, disk d2 
WHERE d1.id > d2.id AND 
      d1.d = d2.d;
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

DELETE disk
FROM disk
INNER JOIN (
       SELECT id,
               d,
               CASE WHEN d = @prevd 
                       THEN @id:=@id+1
                    ELSE @id:=1
               END AS rankNum,
               @prevd:=d AS prd
       FROM disk, (SELECT @prevd:=NULL,@id:=NULL) t
      ) t1
ON disk.id = t1.id
WHERE rankNum >= 2;

For Demo Follow the below link:

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=318e94a135853fcd15b14e4b8bbf1fdc

3 Comments

d is a text field and second thing is it compiled without error but it didnt remove any row... 0 rows affected
CREATE TABLE disk ( id int(11) NOT NULL, title text COLLATE utf8_unicode_ci NOT NULL, link text COLLATE utf8_unicode_ci NOT NULL, mag text COLLATE utf8_unicode_ci NOT NULL, size varchar(10) COLLATE utf8_unicode_ci NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; this my actual table
I edited my answer try it again and show me how it's not working. @user9673975
0

You can do the following..
Creating new table and keeping random row :

  1. first copy table disk(unique data) to temp table disk2.

  2. drop table disk.

  3. rename temp table disk2 to disk.

    create table disk2 select * from disk group by d;
    
    drop table disk;
    
    rename table disk2 to disk;
    

NOTE : Here we using group by with * because OP does not care which row to keep.


Creating new table and keeping row with min or max id : Another way to do this while keeping row with min or max id

/*copy data from disk to temp table disk2*/
create table disk2 select * from disk
    where id in (select min(id) from disk group by d);
/*drop table disk*/
drop table disk;
/*rename temp table to disk*/
rename table disk2 to disk;


UPDATE: Another way to do this
Deleting duplicates from existing table

    /*first create a dups table for duplicates*/
    create table dups select * from disk
        where id not in (select min(id) from disk group by d);
    /*now delete all rows which are present in dups table*/
    delete from disk where id in (select id from dups);
    /*now delete the dups table*/
    drop table dups;

1 Comment

it worked well but still has a problem... my server does not allow me to make more than 5000 changes in the database in an hour. so creating a new table with around 10000 rows and then renaming the table, would not work...
0
delete from disk
where id  in 
(
select id 
from task
group by id 
having count(id) >1
)

2 Comments

Will not delete any rows at all.
Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
0

No need to create any temporary tables

I hope this will work for you

DELETE ColumnName
FROM TableName
INNER JOIN 
(
    SELECT  MAX(ID) AS ID
    FROM TableName
    GROUP BY ID
    HAVING COUNT(*) > 1
) Duplicate on Duplicate.ID = TableName.ID
WHERE TableName.ID < Duplicate.lastId;

Please also check the following link your for more suggestions

MySQL delete duplicate records but keep latest

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.