【发布时间】:2015-12-05 20:38:13
【问题描述】:
我的 MySql 数据库中有 tag 表,如下所示。
Id Title RepeatCount
--------------------------
1 Tag1 1
2 Tag2 5
3 Tag3 8
我想在PDO的表中插入记录,如果不存在具有相同Title的记录(Title是主键),如果存在,增加记录RepeatCount。
像这个例子:
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"
));
【问题讨论】:
-
为什么不使用
INSERT ON DUPLICATE KEY UPDATE检查stackoverflow.com/questions/11390093/… -
只需重新订购:1.
update .. where title = ..2.insert into .. where not in .. -
这就是你要搜索的内容:stackoverflow.com/questions/14797510/…
标签: php mysql if-statement pdo