Mysql: How To Remove Duplicate Entries?

Discussion in 'PHP' started by mmelen, May 11, 2008.

  1. #1
    I want to remove all duplicate entries.
    For example, I have 4 things w/ the name="bubble". I want only 1 to remain.
     
    mmelen, May 11, 2008 IP
  2. apmsolutions

    apmsolutions Peon

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Does it matter which one you keep?
     
    apmsolutions, May 11, 2008 IP
  3. mmelen

    mmelen Well-Known Member

    Messages:
    1,526
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    105
    #3
    not really.
    I guess if i had to choose then the one with the lowest ID number (another field...) but it doesn't really matter.
     
    mmelen, May 11, 2008 IP
  4. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if you mean selecting unique values only, then i suggest you use the SELECT DISTINCT sql command.

    if you actually mean cleaning up your table, maybe you can also use the SELECT DISTINCT command to select distinct records into a dump file and then restore that dump file into a new table.
     
    daboss, May 11, 2008 IP
  5. mmelen

    mmelen Well-Known Member

    Messages:
    1,526
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    105
    #5
    but wouldn't SELECT DISTINCT totally ignore the ones that are duplicates? (not just choose 1...)

    and how would I do SELECT * where word is distinct..

    thanks!
     
    mmelen, May 11, 2008 IP
  6. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    distinct should work and i suggest using the IGNORE command to prevent this from happening again :)
     
    cornetofreak, May 11, 2008 IP
  7. mmelen

    mmelen Well-Known Member

    Messages:
    1,526
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    105
    #7
    Ok... I see what i wanted is to select the whole row when 1 field is distinct.
    Anyway, here it is:
    select *, count(FIELD) from TABLE group by FIELD having count(FIELD)>=1;

    thanks for everyones help.
     
    mmelen, May 11, 2008 IP