Solved

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

Posted on 2013-11-20
4
539 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

16 Experts available now in Live!

Get 1:1 Help Now