BenWeston Posted March 3, 2014 Share Posted March 3, 2014 Hi all. I want to log pageviews per day for each profile ID using this: INSERT INTO pageviews (prof_id,day,views) VALUES ($prof_id,CURDATE(),1) ON DUPLICATE KEY UPDATE views=views+1; The trouble is, I can't make any of the columns unique as none of them will be so how do I make it update, for example, the following entry: Prof ID: 18 Day: 02-23-2014 Views: 1 rather than writing another row with Views: 1 again? Quote Link to comment https://forums.phpfreaks.com/topic/286681-on-duplicate-key-update/ Share on other sites More sharing options...
mac_gyver Posted March 3, 2014 Share Posted March 3, 2014 you would make the combined prof_id,day columns a composite/unique key. Quote Link to comment https://forums.phpfreaks.com/topic/286681-on-duplicate-key-update/#findComment-1471346 Share on other sites More sharing options...
BenWeston Posted March 3, 2014 Author Share Posted March 3, 2014 (edited) Thanks mac_gyver! The problem now is that running this query: INSERT INTO `pageviews` (prof_id , day , views) VALUES ('$prof_id' , CURDATE() , 1) ON DUPLICATE KEY UPDATE views = (views+1); first inserts a new row of 18 - 2014-03-03 - 1 then, when run again, inserts a second row of 18 - 2014-03-03 - 2, then fails to insert any more rows because prof_id and day are indexed and unique. The ON DUPLICATE KEY is looking for all columns to be identical, not just prof_id and day. Edited March 3, 2014 by BenWeston Quote Link to comment https://forums.phpfreaks.com/topic/286681-on-duplicate-key-update/#findComment-1471360 Share on other sites More sharing options...
mac_gyver Posted March 3, 2014 Share Posted March 3, 2014 if would probably help if you posted your table definition. Quote Link to comment https://forums.phpfreaks.com/topic/286681-on-duplicate-key-update/#findComment-1471365 Share on other sites More sharing options...
BenWeston Posted March 3, 2014 Author Share Posted March 3, 2014 Quote Link to comment https://forums.phpfreaks.com/topic/286681-on-duplicate-key-update/#findComment-1471366 Share on other sites More sharing options...
mac_gyver Posted March 3, 2014 Share Posted March 3, 2014 (re)read what someone suggested - you would make the combined prof_id,day columns a composite/unique key. Quote Link to comment https://forums.phpfreaks.com/topic/286681-on-duplicate-key-update/#findComment-1471367 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.