Duplicate entries in mysql database

Discussion in 'MySQL' started by lv211, Nov 17, 2007.

  1. #1
    I'm trying to upload information to my database from a variety of different sources and I'm finding that I'm inserting a lot of duplicate info.

    Is there a way to prevent this from happening?
     
    lv211, Nov 17, 2007 IP
  2. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Check if it exist first I guess

    SQL: select count(column) from table where whatever = "term";

    if count > 0, don't insert, otherwise do
     
    decepti0n, Nov 17, 2007 IP
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Do the items have a unique identifier? If not, consider taking a hash of the values. Then mark one of the fields in the database table "unique" and perhaps use a "ON DUPLICATE KEY" clause in your query to handle dupes.

    deception's suggestion also works but you may want to make it more efficient by returning the entire table and storing the data and then comparing to the stored list instead of querying every time (resource intensive).
     
    krt, Nov 17, 2007 IP
  4. bestsoftworks

    bestsoftworks Peon

    Messages:
    257
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'd go with the unique key idea.

    David
     
    bestsoftworks, Nov 18, 2007 IP