Page 1 of 1

ON DUPLICATE KEY UPDATE -- Query Modification

Posted: Tue Mar 04, 2008 3:52 am
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

Re: ON DUPLICATE KEY UPDATE -- Query Modification

Posted: Tue Mar 04, 2008 8:16 am
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.

Re: ON DUPLICATE KEY UPDATE -- Query Modification

Posted: Tue Mar 04, 2008 11:19 am
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+)