Jump to content

On Duplicate Key Update


BenWeston

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.