Jump to content

[SOLVED] Help with INSERT / ON DUPLICATE UPDATE


phpdolan

Recommended Posts

This should have a simple solution, but I'm not getting it.

 

I need to store employee, monthly schedules in a db. Initially the php query will INSERT, and it will run automatically, so later this query will UPDATE. I thought the following code might work, but it overwrites November with October instead of Inserting a new record. Of course it leaves behind other previously created values for November.

 

INSERT INTO Schedule ( empNum, MonthYr, bidLine ) VALUES ( '1234', 'October 2008', '8011' ) ON DUPLICATE KEY UPDATE bidLine = '8011', empNum = '1234', MonthYr = 'October 2008'

 

The WHERE clause doesn't seem to work in this setting and the manual for 5.0 doesn't include it.

 

What I want is to be able to update bidLine (and 35 other pieces of the row). I think this is dual keys or something like that.

 

How do I accomplish this?

 

David

Link to comment
Share on other sites

Thanks for pointing out that page Barand. I've been up and down it several time and don't see how to write the syntax for 2 key query. I've also checked out the page regarding the syntax for INSERT/ON DUP. That is why I asked for help.

 

I've tried modifying the query a dozen times with mixed results, but never leaving the employee number AND month alone IF they exist and creating a NEW row with those 2 values unique.

 

Do you have another place to point? Or can anyone offer a more descriptive place.

 

David

 

Link to comment
Share on other sites

To anyone interested,

 

If you followed the problem, then the solution was to create a composite key (of empNum and MonthYr) with a different name, and use the following syntax:

 

INSERT INTO Schedule ( empNum, MonthYr, bidLine, col4, col5 ) VALUES ( '1234', 'October 2008', '8011', 'whatever', 'whoever' ) ON DUPLICATE KEY UPDATE bidLine = '8011', col4 = 'whatever, col5 = 'whoever'

 

This way, the empNum and MonthYr are combine to be unique.

 

Happily solved on my own.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.