简体   繁体   中英

Can i INSERT 2 rows or array in mysql at once

我可以一次在mysql中插入2行或数组吗?

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

that means you can use foreach like

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

Yes, of course.

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

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

and another way that works not only in MySQL

INSERT INTO my_table (field1,field2)
SELECT value1, value2
UNION ALL SELECT value3, value4
UNION ALL SELECT value5, value6
$sql = "INSERT INTO table_name (title) VALUES ('row 1'), ('row 2'), ('row 3')";

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

应该使用多个值。

INSER INTO [table] ([columns]) VALUES ([values],[values]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM