1

I have one table named tblm_customer.

It contain field named firstname and lastname. Now I want to delete all records from the table that contain the same firstname and lastname that are already in the table.

I used mysql database and customerid is the primary key in the table.

3
  • Do two records with the same firstname/lastname have also the same customerid? Commented Sep 22, 2010 at 9:19
  • no customerid is unique as it is primary key Commented Sep 22, 2010 at 9:20
  • possible duplicate of How to delete duplicate rows from a MySQL table Commented Sep 16, 2014 at 15:48

1 Answer 1

5

Following delete removes all duplicates, leaving you with the latest CustomerID

A note of warning though. I don't know your use case but it is perfectly possible to have two people with the exact same name (we even had the addres being the same at one time).

DELETE  c1
FROM    tblm_customer c1
        , tblm_customer c2
WHERE   c1.FirstName = c2.FirstName 
        AND c1.LastName = c2.LastName 
        AND c1.CustomerID < c2.CustomerID 
Sign up to request clarification or add additional context in comments.

2 Comments

it will generaERROR 1093 (HY000): You can't specify target table 'TBLM_CUSTOMER' for update in FROM clausete error like
@chetan, I've altered the statement. Previous statement worked with SQL Server. This should work using MySQL.

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.