Solved

How to use MySQL on duplicate key update with unique key?

Posted on 2013-11-20
4
540 Views
Last Modified: 2013-11-20
Hi,

I have a insert on duplicate key update query I want to run to check if there is a record in the sitemap, update it if it exists or insert a new record if it doesn't exist.

Table being queried table structure
In my case the potential duplicate key is a unique text field (url_sef), my query inserts a new record perfectly but does not update an existing one. Can anyone suggest any changes I could make to get this to work or perhaps using a different method all together?

INSERT INTO health3_acesef_sitemap 
(url_sef, published, sdate, frequency, priority, sparent, sorder) 
VALUES ('community-support-services/budgeting-a-financial-services/golden-bay-budget-service', 1, NOW(), 'weekly', '0.5', 0, 1000) ON DUPLICATE KEY UPDATE published=IF(url_sef='community-support-services/budgeting-a-financial-services/golden-bay-budget-service', published=1, published);

Open in new window

0
Comment
Question by:jwleys
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39664775
I haven't done this before myself, but the manual states you need a unique index or primary key on the field with which you want to avoid having duplicates (url_sef in your case). Do you have that at the moment?
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39664794
Also, I think the value specified in your "if" in your query was incorrect. I think it should be:
INSERT INTO health3_acesef_sitemap 
(url_sef, published, sdate, frequency, priority, sparent, sorder) 
VALUES ('community-support-services/budgeting-a-financial-services/golden-bay-budget-service', 1, NOW(), 'weekly', '0.5', 0, 1000) ON DUPLICATE KEY UPDATE published=IF(url_sef='community-support-services/budgeting-a-financial-services/golden-bay-budget-service', 1, published);

Open in new window

0
 

Author Comment

by:jwleys
ID: 39664802
Proof of unique url_sef
Yes the url_sef is set to be a unique

(Storage engine is MyISAM)
0
 

Author Closing Comment

by:jwleys
ID: 39664843
Thanks you were right, url_sef needed to be set to be an index, it was set to be unique but it needed to be unique and an index. You were also right about the query, typo on my behalf.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now