Solved

Delete double rows in mysql database

Posted on 2014-01-28
4
539 Views
Last Modified: 2014-01-28
Hi,

I have a table (table1) with 3 columns, id, text, flag.
The table contains tens of thousands of rows with some text.

Some of these rows are doubles.
I tried this query below to delete these double entries, but it runs for hours and deletes nothing.

DELETE n1 FROM table1 n1, table1 n2 WHERE n1.id > n2.id AND n1.text = n2.text

Does anybody have a better (working) and faster query?

Thanks!
0
Comment
Question by:peps03
  • 2
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39814407
Create an empty table2.

INSERT INTO table2 (id, text, flag) (SELECT id, text, flag FROM table1 GROUP BY table1.text)

HTH,
Dan
0
 

Author Comment

by:peps03
ID: 39814421
Thanks Dan!
That worked! Very quickly!!

What was wrong with my query? A lot?
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39814467
You query looked more like what a programmer would try than what SQL would expect.

You were JOINining your table with itself on the text field, which I guess was not indexed.

It created a huge table and I don't really know what MySQL was trying to do with it.
0
 

Author Comment

by:peps03
ID: 39815088
Ok, thanks!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Read about achieving the basic levels of HRIS security in the workplace.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question