I know this has been asked before, but I'm not a coder, and cannot figure it out from other similar posts. I've spent over 5 hours trying to figure this out with great unsuccess :( So I ask for your help.
1) Prevent Duplicates
I have a PHP script that writes to DB. Here is the code:
$sql = "INSERT INTO results (total, size, persq, strip, material, region)
VALUES ('$total', '$size', '$persq', '$strip', '$material', '$region')";
I want to prevent duplicate rows based on TOTAL and SIZE columns. So if a new entry matches value in TOTAL and SIZE, do not enter new row.
2) Delete Duplicates
I want to delete ALL existing douplicate rows from DB, also based on TOTAL and SIZE columns.
If row contains duplicates in both TOTAL and SIZE, delete entire row.
How do I do this?
PS - I've read that I can use SQL IGNORE command to prevent futue duplicates - example (i've tryed to structure it to work for my situation:
INSERT IGNORE INTO results ...;
would something like this work? If so please help me structure it (i'm new to PHP and MySQL).
Big thanks in advance.
INSERT IGNOREwill only check if the primary key already exists. I don't know if it checks every unique constraint. But you can just try to insert the record and check whether it works. Anyway, Bhavik's advice of adding a unique key is the right starting point (after removing the duplicates, that is)int NOT NULL UNIQUEand use useINSERT IGNOREwill that avoid douplicates? If so, I can just strt from scratch (data in DB is not essential, as it will repopulate fast anyway)