INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=3;
Is it possible to set this so it doesn’t require and entire matching row.
eg: if column ‘a’ is a duplicate then do the update?
thx
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
ON DUPLICATE KEY does just that… if the data you’re inserting violates a unique key requirement, turn it into an update on the row which has the key combination which caused the violation. If your primary key is only one of the fields (e.g. ‘a’), and you already had a row in the table where a=1, then you’d get an update instead and that original row would get its ‘c’ set to 3.
If it’s a composite key (say, ‘a,b’), then if you had an existing records with a=1 and b=2, then that rows’ C would get changed to 3 instead of a new record being created.
If there’s no unique/primary keys on this table, then you’d never get an update, it’d just be an extra-verbose insert statement.