Update a record if it does not exist or if

0

How can I verify if the registry exists and then update it if it does not exist that I insert it?

I tried this, but it does not leave me.

UPDATE tienda SET Campo1 = 'carro' WHERE ID = '123'
IF @@ROWCOUNT = 0
INSERT INTO tienda VALUES ('coche','123')

even with this query

IF EXISTS(SELECT ID FROM tienda WHERE ID = '123')
UPDATE tienda SET Campo1 = 'carro' WHERE ID = '123'
ELSE
INSERT INTO tienda VALUES ('coche','123')

is an example, that's the format I used, I'm using mysql

    
asked by goku venz 05.06.2017 в 15:02
source

1 answer

3

What you need is the statement INSERT ... ON DUPLICATE KEY UPDATE

INSERT INTO tienda  VALUES ('coche', '123') ON DUPLICATE KEY UPDATE campo1 = 'coche'
    
answered by 05.06.2017 / 15:10
source