如何进行多次插入,如:
$query = "INSERT INTO `table` (`fied1`, `field2`, `field3`) VALUES ('one', 'two', 'three'),('four', 'five', 'six');
INSERT INTO `table` (`fied1`, `field2`, `field3`) VALUES ('seven', 'eight', 'nine'),('ten', 'eleven', 'twelve');"
$db->setQuery($query);
$db->query();
我无法拆分此查询,因为我是从大型文本文件中获取的。
这是insert
方法的文档块
/**
* Add a table name to the INSERT clause of the query.
*
* Note that you must not mix insert, update, delete and select method calls when building a query.
*
* Usage:
* $query->insert('#__a')->set('id = 1');
* $query->insert('#__a')->columns('id, title')->values('1,2')->values('3,4');
* $query->insert('#__a')->columns('id, title')->values(array('1,2', '3,4'));
*
* @param mixed $table The name of the table to insert data into.
* @param boolean $incrementField The name of the field to auto increment.
*
* @return JDatabaseQuery Returns this object to allow chaining.
*
* @since 11.1
*/
所以像这样的东西
$query->insert('table')
->columns(`fied1`, `field2`, `field3`)
->values(array('one, two, three', 'four, five, six'));
在网上,我想你可能在第一个字段名上有拼写错误,如果以后我有机会传入一个表示不同字段名数组的变量,我可能会使用$db->quoteName(array('field1', 'field2', field3'))
。