8
id     lat                long     speed      date          address
 1    22.92138131   72.44103313     3.96 km/h     2011-09-26  National, Gujarat, India
 2    22.92138145   72.44103413     13.96 km/h     2011-09-26  National, Gujarat, India
 3    22.92138134   72.44103423     15.96 km/h     2011-09-26  National, Gujarat, India
 4    22.92138454   72.44103233     13.96 km/h     2011-09-26  10t ring Rd, Nehru Nagar
 5    22.92138354   72.44102533     13.96 km/h     2011-09-26  Anandnagar Rd, Ahmedabad
 6    22.92138484   72.44103293     19.96 km/h     2011-09-26  Anandnagar Rd, Ahmedabad

I want to write a query such that my result looks like this:

id     lat                long     speed      date          address
 1    22.92138131   72.44103313     3.96 km/h     2011-09-26  National, Gujarat, India
 4    22.92138454   72.44103233     13.96 km/h     2011-09-26  10t ring Rd, Nehru Nagar
 5    22.92138354   72.44102533     13.96 km/h     2011-09-26  Anandnagar Rd, Ahmedabad

I want to remove duplicate rows according to the address.

5
  • 2
    Please read en.wikipedia.org/wiki/Database_normalization Commented Sep 28, 2011 at 11:29
  • Where an address is duplicated, which row do you wish to return in the resultset? the first by ID? Commented Sep 28, 2011 at 11:31
  • id 2 in not looking duplicate to any other rows.. why you want to remove this also.. Commented Sep 28, 2011 at 11:34
  • is Ian Nelson right ??.. Commented Sep 28, 2011 at 11:35
  • Possible duplicate of Remove duplicate rows in MySQL Commented May 24, 2019 at 10:13

6 Answers 6

9

To check what you are going to delete:

SELECT distinct t1.*
  FROM yourtable as t1
  join yourtable as t2
 WHERE t1.address = t2.address
   and t1.id < t2.id

If you are happy with that:

DELETE t1
  FROM yourtable as t1
  join yourtable as t2
 WHERE t1.address = t2.address
   and t1.id < t2.id

This way you keep the record with the max value on id column

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

2 Comments

what if I need to keep lower id ?
Just change t1.id < t2.id by t1.id > t2.id
4

If you don't care which of the rows you keep

ALTER IGNORE TABLE table ADD UNIQUE KEY 'address' (`address`);

The 'IGNORE' is important, that means to silently ignore duplicate data. (ie ignores it when inserting into the 'new version' of the table.)

May want to remove the index afterwoods

ALTER TABLE table DROP KEY 'address';

Comments

2

Assuming that you wish to return the rows with the smallest ID values:

SELECT 
    *
FROM
TABLENAME T INNER JOIN
(
    SELECT MIN(ID) AS ID FROM TableName 
    GROUP BY Address
) SUB ON T.ID = SUB.ID

Comments

2

Need to create duplicate/temporary table with same field what your current table have.

Then execute below SQL

First clear temporary table :

DELETE FROM `#TMP_TABLE#`;

Insert record in temporary table as per your expectation.

INSERT INTO `#TMP_TABLE#`
SELECT T . *
FROM #TABLE# T
INNER JOIN (
    SELECT MIN( ID ) AS ID
    FROM #TABLE#
    GROUP BY address
    ) SUB ON T.id = SUB.id

Truncate main table

DELETE FROM `#TABLE#`;

Copy Data from temporary table

INSERT INTO #TABLE# SELECT * FROM  `#TMP_TABLE#`

3 Comments

Are you sure is not possible to do by one query? Check my answer, am I missing something?
@DavidEG, yes it is possible. you genius. your solution is perfect one
@maulik.patel, you can use above logic in more complex condition
0
delete from table_name tb where id not in 
   (select min(id) from table_name tb1 group by address)

I assumed that you want to remove rows having address duplicacy and want to keep minimum id's row in your table

Comments

0

You Can Do this Easily by Doing these 3 easy steps and therefore 3 SQL statements:

Step 1: Move the non duplicates (unique tuples) into a temporary table CREATE TABLE new_table as SELECT * FROM old_table WHERE 1 GROUP BY [column to remove duplicates by];

Step 2: delete delete the old table We no longer need the table with all the duplicate entries, so drop it! DROP TABLE old_table;

Step 3: rename the new_table to the name of the old_table RENAME TABLE new_table TO old_table;

For Another Way You Can go to the Bellow Link... Its is Also a good Way... Difficult but with less statements http://dev.mysql.com/doc/refman/5.0/en/insert.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.