无法避免在数据库中重复插入

时间:2014-02-25 07:48:13

标签: mysql sql sqlite

我有一个表格,我在其中添加值,我想避免在我的表格中重复插入,

这是我的问题:

INSERT INTO Categories (Name, Image) VALUES ("Womens Fashion", "http://static.abcd.com/images/nw-cat12-big.png") WHERE Name="Womens Fashion" not in
(
   SELECT Name from Categories WHERE Name="Womens Fashion"
)

错误消息:“WHERE”语法错误附近。

请提前帮助和致谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

INSERT INTO table1 
SELECT * 
FROM   (SELECT 'Womens Fashion'                                 name, 
               'http://static.abcd.com/images/nw-cat12-big.png' images)t1 
WHERE  t1.name NOT IN (SELECT name 
                       FROM   table1); 

SQL Fiddle