INSERT INTO SELECT not working

I need help inserting a few selected columns from table1 into another table2 (with a WHERE clause). The columns that I want to insert are the same in both tables. But, each table has other columns that are not in the other table. For example, table2 has a column called 'neighborhood', which is not in table1. I want to grab nid, ccn, reportdatetime, latitude, longitude, event_type from table1 and put it in table2, and the 'neighborhood' column for this new row should be null.

This is my MySQL:

INSERT INTO table2 
SELECT nid, ccn, reportdatetime, latitude, longitude, event_type 
FROM table1 
WHERE nid=943662

I'm getting this error:

#1136 - Column count doesn't match value count at row 1

Any suggestions on how to get this to work?


Name the columns you're inserting into:

INSERT INTO table2 (nid, ccn, reportdatetime, latitude, longitude, event_type) 
SELECT nid, ccn, reportdatetime, latitude, longitude, event_type 
FROM table1 
WHERE nid=943662;

This will cause nulls to be inserted into columns that aren't explicitly named

链接地址: http://www.djcxy.com/p/71104.html

上一篇: 在mysql中连接3个表时返回零或空值

下一篇: INSERT INTO SELECT不起作用