Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7747025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:25:40+00:00 2026-06-01T10:25:40+00:00

This question here: MySQL: INSERT IGNORE or ON DUPLICATE KEY UPDATE with checking multiple

  • 0

This question here:

MySQL: INSERT IGNORE or ON DUPLICATE KEY UPDATE with checking multiple and not unique columns

Is similar, but not quite.

I am trying to build a watch list, using a table as follows:

    recordId    |    itemId   |   userId    |    dateAdded

The recordId is the only unique column and ss an autoincrement ID field.

itemId, userId and dateAdded can be duplicated individually, but not altogether.

So, if a user has already added an item to his/her watch list, I want it to just update the dateAdded.

Rows therefore may contain the same userId many times (as the user may wish to ‘watch’ multiple items) and it may include the itemId many times (as the same item may be ‘watched’ by many users) but a combination of both the userId and itemId matching a record which already exists should cause an update.

Is there a single statement way to do this, or do I need to ask the database if userId = & postingUser & and itemId = & requestedId & before attempting an insert?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:25:41+00:00Added an answer on June 1, 2026 at 10:25 am

    Here’s how you can do this:

    insert into <tablename> (itemId, userId)
    values (?, ?)
    on duplicate key update dateAdded = now();
    

    where the ?’s stand for the new values you’re inserting

    This is assuming you have defined a unique index for (itemId, userId):

    CREATE TABLE <table> (
      `recordId` int(11) NOT NULL AUTO_INCREMENT,
      `itemId` int(11) DEFAULT NULL,
      `userId` int(11) DEFAULT NULL,
      `dateAdded` timestamp,
      PRIMARY KEY (`recordId`),
      UNIQUE KEY itemuser (`itemId`,`userId`)
    ) ENGINE=InnoDB;
    

    How this works:

    • when you try to insert a row, MySQL will check for key conflicts
    • since you’ve created a unique key for the combination of (itemId, userId), this will be checked
    • if there is no conflict, MySQL will go ahead and insert the row
    • if there is a conflict, MySQL will fall back to the on duplicate key clause, which updates the dateAdded column’s value
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After posting this question MySQL update or insert or die query I've change to
I see this question here on SO but I am not sure that answers
maybe it's not so proper to ask this question here... anyway, I'm trying to
This question is very similar to the question asked here so I hope this
MySQL noob here; looked around first but couldn't find the answer to this question.
This question is a continuation of my previous question here zend models architecture (big
This question is sort of a follow-up to my original question here . Let's
As an extension to this question here Linking JavaScript Libraries in User Controls I
I posted this question here on purpose, although - for my case - it
It might be a long shot posting this question here but we will see.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.