ON DUPLICATE KEY UPDATE -- Query Modification

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
waqas_punjabian
Forum Commoner
Posts: 67
Joined: Wed Aug 10, 2005 9:53 am

ON DUPLICATE KEY UPDATE -- Query Modification

Post by waqas_punjabian »

Hi all,

Please have a look on following query:

Code: Select all

INSERT INTO league (ut,url,game,eID) VALUES (now(),'$url','s',$lid) ON DUPLICATE KEY UPDATE ut=VALUES(ut), url=VALUES(url)
As per my knowledge, "ON DUPLICATE KEY UPDATE" works in this way, "If record not exist then INSERT otherwise Update the existing record with reference of the Primary Key or Unique ID".

Here's a question:

I want to check IF the given URL is same as the existing URL then do nothing, otherwise INSERT or UPDATE.
Is it possible to modify the query to achieve this goal?

looking forward for your kind response,
WAQAS
able
Forum Newbie
Posts: 4
Joined: Sun Mar 02, 2008 9:53 pm

Re: ON DUPLICATE KEY UPDATE -- Query Modification

Post by able »

I don't know how to do what you want, but I'd really like to know if its possible in a single query too.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: ON DUPLICATE KEY UPDATE -- Query Modification

Post by jmut »

Assume we talk about mysql. If what data you're about to save already exists... mysql won't make the query anyhow. If it is new...insert will go.
You can see that by examinig mysql log while running script.
Else for anything more complex you can make mysql function/procedure for this (in mysql 5+)
Post Reply