如何进行多次插入,例如:
$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
方法的文档 block
/**
* 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'))
如果有稍后我有机会传入一个代表不同字段名称数组的变量。
关于php - 一个查询中的多个插入 Joomla,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31192372/