While executing an INSERT
statement with many values, I want to update duplicate entries that contain the same data within two or more values.
After some research ON DUPLICATE KEY UPDATE
seems to be the way to go. However I am struggling actually implementing it, even after reading numerous similar threads.
Am I right in this assumption? What's the best way to go about replacing data if the values are the same in two or more rows?
Steps that I have taken:
-Created my database
CREATE TABLE IF NOT EXISTS installs (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
userid INT UNSIGNED NOT NULL DEFAULT '0',
email varchar(255) NOT NULL,
addontitle varchar(255) NOT NULL,
addonversion varchar(255) NOT NULL,
bburl varchar(255) NOT NULL,
bbtitle varchar(255) NOT NULL,
webmasteremail varchar(255) NOT NULL,
cookie varchar(255) NOT NULL,
dateline INT UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (id),
KEY userid (userid)
)
-Inserted Data via PHP script:
INSERT INTO installs (
username,userid,email,addontitle,addonversion,bburl,bbtitle,webmasteremail,cookie,dateline
) VALUES (
'$username','$userid','$email','$addontitle','$addonversion','$bburl','$bbtitle','$webmasteremail','$cookie',NOW()
)
ON DUPLICATE KEY UPDATE
addontitle = VALUES($addontitle)
This doesn't quite get me the results I need though.
So as an example here are a few submitted rows:
1 - Bob - 25 - bob@test.com - Addon 1 - 1.0 - test.com - Test Site - admin@test.com - 13 - DATE
2 - Bob - 25 - bob@test.com - Addon 2 - 1.0 - test.com - Test Site - admin@test.com - 13 - DATE
3 - Bob - 25 - bob@test.com - Addon 2 - 1.2 - site.com - Some Site - admin@test.com - 13 - DATE
If an insert were to contain the following values, I'd like to be able to UPDATE row 1 since $bburl
and $addontitle
match.
4 - Bob - 25 - bob@test.com - Addon 1 - 2.0 - test.com - Test Site - admin@test.com - 13 - DATE
Then the table should read:
1 - Bob - 25 - bob@test.com - Addon 1 - 2.0 - test.com - Test Site - admin@test.com - 13 - NEWDATE
2 - Bob - 25 - bob@test.com - Addon 2 - 1.0 - test.com - Test Site - admin@test.com - 13 - DATE
3 - Bob - 25 - bob@test.com - Addon 2 - 1.2 - site.com - Some Site - admin@test.com - 13 - DATE
Is this type of operation possible to achieve using ON DUPLICATE KEY UPDATE
? Is there a better method?