Check then Add / Update
am 09.08.2006 19:11:33 von Peter Lewis
Hi there
Is it possible in one statement to check if an entry is there, if so update
it if not then add it.
I can do a normal by running a statement first but for reasons to long to go
into I really would
like it to all be done in one SQL statement
e.g.
if THIS is THERE then UPDATE IT WITH THIS if not there then ADD THIS NEW
ENTRY
Brian
Re: Check then Add / Update
am 10.08.2006 00:04:09 von zac.carey
Brian wrote:
> Hi there
>
> Is it possible in one statement to check if an entry is there, if so update
> it if not then add it.
> I can do a normal by running a statement first but for reasons to long to go
> into I really would
> like it to all be done in one SQL statement
>
> e.g.
>
> if THIS is THERE then UPDATE IT WITH THIS if not there then ADD THIS NEW
> ENTRY
>
> Brian
Have a look at INSERT... ON DUPLICATE KEY UPDATE
Basically, as long as you have a UNIQUE or PRIMARY KEY on the
duplicatable fields then this syntax should work:
INSERT INTO table(field_a,field_b,field_c) VALUES
('value_a','value_b','value_c')
ON DUPLICATE KEY UPDATE a='value_a',b='value_b','c=value_c';