nemovc Posted January 17, 2012 Share Posted January 17, 2012 Hi I have a table 'contributions' CREATE TABLE `op_contributions` ( `id` int(11) NOT NULL AUTO_INCREMENT, `attachID` int(11) NOT NULL, `playerID` int(11) NOT NULL, `a` int(11) NOT NULL, `b` int(11) NOT NULL, `c` int(11) NOT NULL, `d` int(11) NOT NULL, `e` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 The playerID is the ID (primary key) of a row in another table (players), and the attachID is the ID (primary key) of a row in another table (plans). (a to e are data fields). attachID and playerID don't have to be unique, but the combination of them has to be. E.g., there are many different plans, and each has a different players contributing different amounts, but each player can only contribute once to each plan. I am unsure how to create a INSERT ... ON DUPLICATE UPDATE query when it is the combination of two columns that is important, not a singular column. (At the moment I am using the messy workaround of loading the all the plans into php and then either UPDATE or INSERT depending on if a record is already found) Response greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/255229-unsure-on-sql-for-insert-on-duplicate-update-with-two-columns/ Share on other sites More sharing options...
fenway Posted January 17, 2012 Share Posted January 17, 2012 You just need to have your unique key span both columns. Quote Link to comment https://forums.phpfreaks.com/topic/255229-unsure-on-sql-for-insert-on-duplicate-update-with-two-columns/#findComment-1308613 Share on other sites More sharing options...
ManiacDan Posted January 17, 2012 Share Posted January 17, 2012 Make another unique index for that combination of columns. The "on duplicate" condition will trigger when a unique key (primary or otherwise) is violated. Quote Link to comment https://forums.phpfreaks.com/topic/255229-unsure-on-sql-for-insert-on-duplicate-update-with-two-columns/#findComment-1308614 Share on other sites More sharing options...
nemovc Posted January 18, 2012 Author Share Posted January 18, 2012 Thanks guise, I have got this working now. I didn't know how indexes spanning multiple columns worked... but I do now, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/255229-unsure-on-sql-for-insert-on-duplicate-update-with-two-columns/#findComment-1308811 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.