MySql 插入数据前判断数据是否存在
原创
©著作权归作者所有:来自51CTO博客作者indigotang的原创作品,请联系作者获取转载授权,否则将追究法律责任
语法格式:
INSERT INTO table(field1, field2, fieldn) SELECT 'field1',
'field2', 'fieldn' FROM DUAL WHERE NOT EXISTS(SELECT field FROM
table WHERE field = ?)
例子:
INSERT INTO t_base_employee(EmpName,EmpNum,EmpPwd,EmpBase,IsAdmin)
SELECT '张三','100001','100001','基地名称',0
FROM DUAL
WHERE NOT EXISTS(SELECT * FROM t_base_employee WHERE EmpNum = '100001');