mysql插入前判断数据是否存在

 

转:https://blog.csdn.net/lai0yuan/article/details/78234102

可以插入前进行判断要插入的数据是否存在业务代码如下 :

INSERT INTO t_topic_vistor(user_id,topic_code) SELECT '218', 
'XQ33019920170811142528217' FROM DUAL WHERE NOT EXISTS(SELECT * 
FROM t_topic_vistor WHERE user_id = 218 and 
topic_code='XQ33019920170811142528217')
  •  

语法如下:

INSERT INTO table(field1, field2, fieldn) SELECT 'field1', 
'field2', 'fieldn' FROM DUAL WHERE NOT EXISTS(SELECT field FROM 
table WHERE field = ?)

你可能感兴趣的:(mysql数据库)