MySQL:仅在插入时插入

时间:2012-04-05 12:41:26

标签: mysql

我想仅在查询的两个值(或更多)不同时才插入行:

INSERT INTO table (id, title, description) 
VALUES(1, 'hello', 'world') 
ONLY IF title AND description DOESN't ALREADY EXISTS

2 个答案:

答案 0 :(得分:2)

INSERT INTO TABLE (ID, TITLE, DESCRIPTION) VALUES (1, 'hello', 'world')
WHERE (SELECT COUNT(ID) FROM TABLE WHERE TITLE='hello' AND DESCRIPTION='world') = 0

答案 1 :(得分:0)

如果您在两列

上添加唯一键,我认为您可以使用replace into
REPLACE INTO table (id, title, description) 
VALUES(1, 'hello', 'world')