[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2013-11-20
4
Medium Priority
?
549 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

650 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