INSERT插入WHERE判断是否插入(MySQL)

一、INSERT INTO IF EXISTS 具体语法:
INSERT INTO table(field1, field2, fieldn) SELECT ‘field1‘, ‘field2‘, ‘fieldn‘ FROM DUAL WHERE NOT EXISTS(SELECT field FROM table WHERE field = ?);


二、例子说明(DUAL是虚拟表)
INSERT INTO card(cardno, cardnum) SELECT ‘111‘, ‘100‘ FROM DUAL WHERE NOT EXISTS(SELECT cardno FROM card WHERE cardno = ‘111‘);
INSERT INTO card(cardno, cardnum) SELECT ‘222‘, ‘200‘ FROM DUAL WHERE NOT EXISTS(SELECT cardno FROM card WHERE cardno = ‘222‘);

 

INSERT插入WHERE判断是否插入(MySQL)

上一篇:关于MySQL的关联查询


下一篇:MySQL学习笔记超级详细 从0开始