0

I have 2 columns date(primary) and user_id(unique). I want to check if the row exists if not create it, if so update the row for that day.

for example: if a user updates the database multiple times a day I only want 1 record created per day and that record to update every time the user updates the database. However if the same user(same user_id) updates the database the next day I want a new record created.

I'm using the following code:

INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;

The problem is it doesn't appear to be checking for both date AND user_id but only the user_id. How can I get it to check for both? Thanks.

1 Answer 1

0

I think you want a single unique index on the table. Drop your current indexes and define:

create unique index idx_table_user_date on table(user, date)

This should give you the behavior that you want -- multiple users and multiple dates in the table, but no users have the same date twice (or dates have the same user twice).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.