INSERT INTO table (k1,k2,k3)
VALUES
(v1,v2,v3),
(v1,v2,v3),
(v1,v2,v3)结果最终生成的是INSERT INTO table (k1,k2,k3)
SELECT v1,v2,v3 UNION ALL SELECT v1,v2,v3 UNION ALL SELECT v1,v2,v3看了下源码 public function insertAll($dataSet, $options)
{
// 获取合法的字段
if ('*' == $options['field']) {
$fields = array_keys($this->query->getFieldsType($options));
} else {
$fields = $options['field'];
}
foreach ($dataSet as &$data) {
foreach ($data as $key => $val) {
if (!in_array($key, $fields, true)) {
if ($options['strict']) {
throw new Exception('fields not exists:[' . $key . ']');
}
unset($data[$key]);
} elseif (is_scalar($val)) {
$data[$key] = $this->parseValue($val, $key);
} else {
// 过滤掉非标量数据
unset($data[$key]);
}
}
$value = array_values($data);
$values[] = 'SELECT ' . implode(',', $value);
}
$fields = array_map([$this, 'parseKey'], array_keys(reset($dataSet)));
$sql = str_replace(
['%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
[
$this->parseTable($options['table'], $options),
implode(' , ', $fields),
implode(' UNION ALL ', $values),
$this->parseComment($options['comment']),
], $this->insertAllSql);
return $sql;
}最多执行结果是: #1222 - The used SELECT statements have a different number of columns不明白批量插入为何要生成这个类型的SQL语句,希望能有人给解惑一下 最佳答案