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? 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. 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 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. 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. 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 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. Link to comment https://forums.phpfreaks.com/topic/286681-on-duplicate-key-update/#findComment-1471367 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.