2010-11-08 30 views

回答

4
insert into table (name,other) values ('name1', 'other2'), ('name2', 'other2'), ('name3', 'other3') 

,这意味着你可以使用的foreach像

$values = Array(); 
foreach($array as $arr) 
    $values[] = "('{$arr['name']}','{$arr['name']}')"; 
query("insert into table values" . implode(', ', $values)); 
+0

投票。这是一个很好的答案。 – faressoft 2010-11-08 13:04:45

+0

你为什么要连接? '$ values []。=“('' - 这绝对没有意义,而且一旦你自己编写了循环,为什么在将它放入查询之前没有将字符串转义? – AlexanderMP 2010-11-08 13:08:19

+0

起初我认为是串联的。在换成阵列后,我只是忘记了。是的,最好在这里逃跑。 – nerkn 2010-11-08 13:26:44

3

是的,当然。

http://dev.mysql.com/doc/refman/5.1/en/insert.html

INSERT INTO my_table (field1,field2) VALUES (value1, value2), (value3, value4) 

和作品不仅在MySQL

INSERT INTO my_table (field1,field2) 
SELECT value1, value2 
UNION ALL SELECT value3, value4 
UNION ALL SELECT value5, value6 
+0

这就是我需要的。谢谢。 – faressoft 2010-11-08 12:54:31

1

应具有多个值工作的另一种方式。

INSER INTO [table] ([columns]) VALUES ([values],[values]); 
相关问题