删除旧记录并在一个查询mysql中插入新记录

时间:2014-06-28 14:19:35

标签: mysql

有没有办法从表中删除旧记录并使用mysql在一个查询中插入新记录?

例如,将这2个查询合并为一个。

DELETE FROM A where customer_id = 5;

INSERT INTO A (product_id, customer_id) VALUES (1, 5);
INSERT INTO A (product_id, customer_id) VALUES (5, 5);
INSERT INTO A (product_id, customer_id) VALUES (12, 5);
INSERT INTO A (product_id, customer_id) VALUES (543, 5);

1 个答案:

答案 0 :(得分:0)

删除和插入并不意味着在一个查询中。但是,您可以将插入合并到单个查询中。这将是脚本:

DELETE FROM A where customer_id = 5;

INSERT INTO A (product_id, customer_id) VALUES (1, 5), (5, 5), (12, 5), (543, 5);