0

I have a mysql table called urls

It has the following fields

url
title (default null)
content (default null)

My table has many duplicate rows.

I would like to remove those duplicates.

For example

google.com - Google - search engine
google.com - null - null
google.com - Google - search engine

Now I would like to drop the rows where title = null plus the duplicate row.

I just want the url to be unique with no duplicate rows.

Can some tell me how?

3
  • Does this table have a primary key field ID ? Commented Aug 19, 2013 at 6:07
  • No.. url is the primary key Commented Aug 19, 2013 at 6:09
  • @Giri, for begin, mark your url field as unique Commented Aug 19, 2013 at 6:15

3 Answers 3

3

This is the simplest:

create tableB like tableA;
INSERT INTO tableB SELECT DISTINCT * FROM tableA WHERE title IS NOT NULL;
DROP tableA;
RENAME tableB TO tableA;

Even in a production environment the drop and rename is usually quick enough to be near invisible to any users. Double check to make sure you end up with the appropriate indexes.

Sign up to request clarification or add additional context in comments.

Comments

0

you can add url as the unique index.the following query will add the unique index and remove the deuplicates.

alter ignore table urls add unique index(url);

If you dont want to add unique index, then the alternate is to create a temp table with unique index,copy the data,remove the duplicate and transfer back to your original table.

Comments

0
    DELETE FROM urls WHERE title IS NULL 
           OR url IN (SELECT a.URL from urls a, urls b WHERE a.url=b.url);

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.