我有一个像这样的table1:
Date Reading Cost
2009-01-01 5.00 1500.00
2009-01-02 10.00 9800.33
...
我想制作一个像这样的新表:
MeasureDate Class Date Reading Cost
2010-01-01 One 2009-01-01 5.00 1500.00
2010-01-01 One 2009-01-02 10.00 9800.33
...
我以为我可以使用类似的东西:
insert into table2 (MeasureDate, Class, Date, Reading, Cost)
values ("2010-01-01", "One", (select * from table1))
但是我收到一条错误消息,表明列数不匹配:
Column count doesn't match value count at row 1
有什么建议吗?感谢。
答案 0 :(得分:1)
尝试
insert into table2 (MeasureDate, Class, Date, Reading, Cost)
select "2010-01-01", "One", Date, Reading, Cost from table1;
答案 1 :(得分:1)
两种方法。
1)清洁解决方案:
INSERT INTO table2 (`MeasureDate`, `Class`, `Date`, `Reading`, `Cost`)
SELECT "2010-01-01", "One", `Date`, `Reading`, `Cost` FROM table1;
注意正确转义,如果使用Date
等字段名称,请注意更多。
2)快速和肮脏的解决方案:
INSERT INTO table2 ()
SELECT "2010-01-01", "One", * FROM table1;