Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道如何在单个查询中插入具有常量值的多行:
INSERT INTO table VALUES (1, 'a', 'x'), (2, 'b', 'y'), (3, 'c', 'z');
但是我如何有条件地做到这一点?我想确保只插入不存在的*行。甚至可以在单个查询中完成吗?
*当所有列与我们插入的列相同时,该行存在
使用INSERT IGNORE INTO而不是INSERT INTO. 如果没有违反唯一键约束,前者只会插入行。
INSERT IGNORE INTO
INSERT INTO
INSERT IGNORE INTO table VALUES (1, 'a', 'x'), (2, 'b', 'y'), (3, 'c', 'z');