[英]Moving a row from one table to another (Insert value list does not match column list)
I am using Laravel PHP framework's Fluent query builder to move rows from one table to another. 我正在使用Laravel PHP框架的Fluent查询生成器将行从一个表移动到另一个表。 PDO is being used.
正在使用PDO。 While using the raw query
DB::query()
, I get the error: 使用原始查询
DB::query()
,出现错误:
Error 错误
SQLSTATE[21S01]: Insert value list does not match column list:
1136 Column count doesn't match value count at row 1
SQL: INSERT IGNORE into listings_archive VALUES (?, ?)
Query 询问
// Get rows from first table
$rows = DB::table('table_1')
->where('id', '>', '12345')
->get();
// Copy rows to second table
foreach($rows as $row) {
$listing = get_object_vars($row);
DB::query('INSERT IGNORE into table_2 VALUES (?, ?)', $row);
}
var_dump of $row $ row的var_dump
array(39) {
["id"]=>
string(7) "2511877"
["name"]=>
string(2) "AB"
["color"]=>
NULL
["type"]=>
NULL
...
What is causing the error and how can it be fixed? 是什么导致错误,如何解决? I tried removing the elements with
NULL
s but still get the same error! 我尝试使用
NULL
删除元素,但仍然收到相同的错误!
This is possibly a problem with the array being passed into DB::query()
. 将数组传递到
DB::query()
可能是一个问题。 These very simple example gave similar errors: 这些非常简单的示例给出了类似的错误:
$row = array('id', 123);
DB::query('INSERT IGNORE into table_2 VALUES (?, ?)', $row);
and 和
$row = array('id' => 123);
DB::query('INSERT IGNORE into table_2 VALUES (?, ?)', $row);
Error 错误
SQLSTATE[21S01]: Insert value list does not match column list:
1136 Column count doesn't match value count at row 1
Well the column count doesn't match the number of values. 好吧,列数与值的数量不匹配。 If you don't specify which columns, it defaults to all, so you want something like
如果您不指定哪些列,则默认为所有列,因此您需要
Insert IGNORE into table_2(Column1,Column2) Values (?, ?)
Column1, Column2, being the names of the two columns in table_2 you want to put the values from Table_1 into. Column1和Column2是要将Table_1中的值放入table_2中的两列的名称。
Had the same Error msg, but i used mysqli in php 有相同的错误消息,但我在PHP中使用mysqli
Solved by some tinkering: 通过一些修补解决:
$stmt = $con->prepare("INSERT INTO table2
(field1, field2, ..., )
SELECT
field1, field2, ...,
FROM
table1 WHERE field = ? ");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.