Insert a record if it EXISTS otherwise UPDATE it MYSQL

1

Good friends I hope you can help me, I have the following table

 CREATE TABLE 'almacen_articulo' (
'id' INT(4) NOT NULL AUTO_INCREMENT,
'existencia' VARCHAR(5) NOT NULL DEFAULT '0',
'id_articulo' VARCHAR(4) NOT NULL DEFAULT '0',
'id_almacen' VARCHAR(4) NOT NULL DEFAULT '0',
'existencia_comprome' VARCHAR(5) NOT NULL DEFAULT '0',
PRIMARY KEY ('id')
 )
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
AUTO_INCREMENT=4
;

these are the data they contain

My intention is to make a insert of article 85 in store 2 if not, otherwise if it already exists in that store update its existence

I have tried this query but keep doing the insert instead of updating it

INSERT INTO almacen_articulo (id_articulo,id_almacen,existencia) VALUES(85, 2, 100) ON DUPLICATE KEY UPDATE id_articulo=85, id_almacen=2, existencia=100
    
asked by David 06.10.2017 в 05:33
source

1 answer

0

For it to work, you would have to define the fields article_id and id_almacen as UNIQUE or PRIMARY KEY . Here is the info of ON DUPLICATE KEY UPDATE link

    
answered by 06.10.2017 / 09:56
source