samedi 5 décembre 2015

Update record if "title" already exists in table when inserting

I have tag table in my MySql db like this.

Id    Title    RepeatCount
--------------------------
 1     Tag1        1
 2     Tag2        5
 3     Tag3        8

I want to insert record into table in PDO, If record with same Title not exist (Title is primary key) and if exist, Increase record RepeatCount.

Like this example:

prepare(
    "IF (:Title IN (SELECT Title FROM tag)) 
     THEN INSERT INTO tag (Title) VALUES (:Title)
     ELSE UPDATE tag SET RepeatCount = RepeatCount + 1"
);
execute(array(
    ":Title" => "MyTag"
));

Aucun commentaire:

Enregistrer un commentaire