MySQL在1个查询中执行多个INSERT

时间:2012-10-10 09:59:31

标签: php mysql database

嘿伙计们,我想知道每次查询一次可以使用多于1个INSERT吗?或者每次INSERT必须一次完成一次?

例如,我想将这两个查询作为一个执行以节省服务器资源:

INSERT INTO ExtraStats (date, supportStaff, startEmails, endEmails, emailsAnswered) VALUES ('2012-09-01', '5', '4', '3', '2') ON DUPLICATE KEY UPDATE supportStaff = '5', startEmails = '4', endEmails = '3', emailsAnswered = '2'
INSERT INTO ExtraStats (date, supportStaff, startEmails, endEmails, emailsAnswered) VALUES ('2012-09-2', '5', '6', '7', '8') ON DUPLICATE KEY UPDATE supportStaff = '5', startEmails = '6', endEmails = '7', emailsAnswered = '8'

有没有合并它们?我试过把它们放在同一个查询中,但它不喜欢它。

3 个答案:

答案 0 :(得分:1)

是的,你可以试试这个

INSERT INTO tbl_name(col1,col2,col3) VALUES ('aaa','bbb','ccc'),('ddd','eee','fff');

或者你可以在第一次查询后加上分号并继续第二次查询。

希望这有帮助。

答案 1 :(得分:1)

INSERT INTO ExtraStats (date, supportStaff, startEmails, endEmails, emailsAnswered) 
VALUES ('2012-09-01', '5', '4', '3', '2') 
ON DUPLICATE KEY UPDATE supportStaff = '5', startEmails = '4', endEmails = '3', emailsAnswered = '2';

('2012-09-2', '5', '6', '7', '8') 
ON DUPLICATE KEY UPDATE supportStaff = '5', startEmails = '6', endEmails = '7', emailsAnswered = '8'

答案 2 :(得分:0)

以分号分隔“;”喜欢:

INSERT INTO ExtraStats (date, supportStaff, startEmails, endEmails, emailsAnswered) 
VALUES ('2012-09-01', '5', '4', '3', '2') ON DUPLICATE KEY UPDATE supportStaff = '5', 
startEmails = '4', endEmails = '3', emailsAnswered = '2';

INSERT INTO ExtraStats (date, supportStaff, startEmails, endEmails, emailsAnswered) VALUES 
('2012-09-2', '5', '6', '7', '8') ON DUPLICATE KEY UPDATE supportStaff = '5', startEmails = 
'6', endEmails = '7', emailsAnswered = '8';