INSERT INTO SELECT不起作用

Lax*_*idi 1 mysql sql select insert-into

我需要帮助将table1中的一些选定列插入另一个table2(带有WHERE子句).我要插入的列在两个表中都是相同的.但是,每个表都有其他列不在另一个表中.例如,table2有一个名为'neighborhood'的列,它不在table1中.我想从table1中获取nid,ccn,reportdatetime,latitude,longitude,event_type并将其放在table2中,并且此新行的"邻域"列应为null.

这是我的MySQL:

INSERT INTO table2 
SELECT nid, ccn, reportdatetime, latitude, longitude, event_type 
FROM table1 
WHERE nid=943662
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

#1136 - Column count doesn't match value count at row 1
Run Code Online (Sandbox Code Playgroud)

有关如何使其工作的任何建议?

Boh*_*ian 14

命名要插入的列:

INSERT INTO table2 (nid, ccn, reportdatetime, latitude, longitude, event_type) 
SELECT nid, ccn, reportdatetime, latitude, longitude, event_type 
FROM table1 
WHERE nid=943662;
Run Code Online (Sandbox Code Playgroud)

这将导致将空值插入未明确命名的列中