简体   繁体   English

将行从一个表移动到另一个表(插入值列表与列列表不匹配)

[英]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删除元素,但仍然收到相同的错误!


UPDATE 更新

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

  • basically as stated above, if tables not identical in structure, you gotta name the fields 基本上如上所述,如果表的结构不同,则必须命名字段

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.

相关问题 错误:插入值列表与列列表不匹配:1136列计数与第1行的值计数不匹配 - Error: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1 插入值列表与列列表不匹配:1136 列计数与第 1 行的值计数不匹配 - 但数字相同 - Insert value list does not match column list: 1136 Column count doesn't match value count at row 1 - But the numbers are the same PDO未捕获的异常-插入值列表与列列表不匹配 - PDO Uncaught exception - Insert value list does not match column list 插入值列表与列列表不匹配,语句与DB是否对应? - Insert value list does not match column list, statement and DB are corresponding? 插入值列表与列列表不匹配:1136 列计数与值计数不匹配 - Insert value list does not match column list: 1136 Column count doesn't match value count MySql语句错误:SQLSTATE [21S01]:插入值列表与列列表不匹配:1136列计数与第1行的值计数不匹配 - MySql statement Error: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1 PDO错误:致命错误:插入值列表与列列表不匹配 - PDO error: Fatal error: Insert value list does not match column list 如何将下拉列表中的选定值插入另一个 MySQL 表 - How to insert selected value from dropdown list into another MySQL table Laravel多个插入无法正常工作时显示“插入值列表不匹配” - Laravel Multiple insert not working says “insert value list does not match ” 将值从一个表插入到另一个表 - insert value from one table to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM