1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

query for delete duplicate data

Discussion in 'MySQL' started by dineshsingh1984, Sep 17, 2014.

  1. #1
    my table is test
    id name
    -------------------------
    1 prince
    2 king
    3 queen
    4 prince
    5 prince
    6 prince


    I want to delete repeated name like prince repeated many times. so i want to remove repeated data and remain only one time every name.

    my query is :
    $sql = mysql_query("delete from test where id in (select id from (SELECT id FROM test GROUP BY name having COUNT(*) > 1)temp");
    but it's not working........
    plz help me................
     
    dineshsingh1984, Sep 17, 2014 IP
  2. pmf123

    pmf123 Notable Member

    Messages:
    1,447
    Likes Received:
    75
    Best Answers:
    0
    Trophy Points:
    215
    #2
    why not create a temp table and just to a INSERT INTO newdatabase ('name') SELECT DISTINCT name from database

    your query looks like it will delete all duplicates
     
    pmf123, Sep 17, 2014 IP
  3. Krellen

    Krellen Greenhorn

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #3
    I think you should consider altering your table and adding a unique key restraint on the name column.

    Then you could do as pmf123 suggests to populate that new table.

    That would solve your current problem and keep this issue from recurring in the future.
     
    Krellen, Sep 17, 2014 IP
  4. www_HypeFree_com

    www_HypeFree_com Active Member

    Messages:
    218
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    65
    #4
    One way is to create a view with no duplicates then put it back into the cleaned out table. There are other ways I'm a bit rusty use to do this years ago.
     
    www_HypeFree_com, Sep 18, 2014 IP
  5. Taruna P

    Taruna P Greenhorn

    Messages:
    13
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #5
    DELETE E1 from TEST E1, TEST E2 where e1.name = e2. name and e1.id > e2.id
     
    Taruna P, Sep 19, 2014 IP
  6. Krellen

    Krellen Greenhorn

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #6
    There are lots of ways to do it, as you can see. It all depends on if you need to do it more than once and fix the problem permanently going forward, of if you just want to do a one-time cleanup.
     
    Krellen, Sep 19, 2014 IP
  7. tuiykkwr

    tuiykkwr Active Member

    Messages:
    106
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #7
    You can use distinct keyword in your query to retrieve only unique values.
     
    tuiykkwr, Oct 22, 2014 IP