Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4098542
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:18:56+00:00 2026-05-20T20:18:56+00:00

I have a table with 8 columns in, but over time I have picked

  • 0

I have a table with 8 columns in, but over time I have picked up numerous duplicates. I have looked at the other question with a similar topic, but it does not solve the issue I am currently having.

+---------------------------------------------------------------------------------------+
| id | market | agent | report_name | producer_code | report_date | entered_date | sync |
+---------------------------------------------------------------------------------------+

What defines a unique entry is based on the market, agent, report_name, producer_code, and report_date fields. What I am looking for is a way to list all the duplicate entries and delete them. Or to just delete the duplicate entries.

I have thought about doing it with a script, but the table contains 2.5mil entries, and the time it would take would be unfeasible.

Could anybody suggest any alternatives? I have seen people get a list of duplicates using the following query, but not sure on how to adapt it to my situation:

SELECT id, count(*) AS n
 FROM table_name
GROUP BY id
HAVING n > 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:18:57+00:00Added an answer on May 20, 2026 at 8:18 pm

    Here are two strategies you might think about. You will have to adjust the columns used to select duplicates based upon what you actually consider a duplicate. I just included all of your listed columns other than the id column.

    The first simply creates a new table without duplicates. Sometimes this is actually faster and easier than trying to delete all the offending rows. Just create a new table, insert the unique rows (I used min(id) for the id of the resulting row), rename the two tables, and (once you are satisfied that everything worked correctly) drop the original table. Of course, if you have any foreign key constraints you’ll have to deal with those as well.

    create table table_copy like table_name;
    
    insert into table_copy
    (id, market, agent, report_name, producer_code, report_date, entered_date, sync)
    select min(id), market, agent, report_name, producer_code, report_date, 
           entered_date, sync
    from table_name
    group by market, agent, report_name, producer_code, report_date, 
             entered_date, sync;
    
    RENAME TABLE table_name TO table_old, table_copy TO table_name;
    
    drop table table_old;
    

    The second strategy, which just deletes the duplicates, uses a temporary table to hold the information about what rows have duplicates since MySQL won’t allow you to select from the same table you are deleting from in a subquery. Simply create a temporary table with the columns that identify the duplicates plus an id column that will actually hold the id to keep and then you can do a multi-table delete where you join the two tables to select just the duplicates.

    create temporary table dups
    select min(id), market, agent, report_name, producer_code, report_date, 
           entered_date, sync
    from table_name
    group by market, agent, report_name, producer_code, report_date, 
             entered_date, sync
    having count(*) > 1;
    
    delete t 
    from table_name t, dups d
    where t.id != d.id
    and t.market = d.market
    and t.agent = d.agent
    and t.report_name = d.report_name
    and t.producer_code = d.producer_code
    and t.report_date = d.report_date
    and t.entered_date = d.entered_date
    and t.sync = d.sync;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table where one column has duplicate records but other columns are
I have a table with columns ID, DateStamp and the ID need not be
I have a table with columns Index, Date where an Index may have multiple
I have a table with three columns: user varchar, status varchar , rep int
I have a table with four columns: PartNumber, ValvePartNumber, ActuatorPartNumber, Price I want to
I have a table with arbitrary columns and rows. This fact is irrelevant though
Let's say I have a table tbl with columns id and title . I
I have a table y Which has two columns a and b Entries are:
I have a table with say 3 columns. There's no primary key so there
I have a table that holds only two columns - a ListID and PersonID.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.