开发者

Create new table with data from other table and labels

I have a table1 like this:

Date             Reading      Cost
2009-01-01      5.00            1500.00
2009-01-02      10.00           9800.33
...

I would like to make a new table like this:

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
...

I assumed I could use something like:

insert into table2 (MeasureDate, Class, Date, Reading, Cost)
values ("2010-01-01", "One", (select * from table1))

However I get an error message that the number of columns don't match:

Column count doesn't match value count at row 1

Any suggestions? Thanks.


Try

insert into table2 (MeasureDate, Class, Date, Reading, Cost)
   select "2010-01-01", "One", Date, Reading, Cost from table1;


Two approaches.

1) The clean solution:

INSERT INTO table2 (`MeasureDate`, `Class`, `Date`, `Reading`, `Cost`)
SELECT "2010-01-01", "One", `Date`, `Reading`, `Cost` FROM table1;

Watch out for proper escaping, even more if using field names like Date.

2) the quick&dirty solution:

INSERT INTO table2 ()
SELECT "2010-01-01", "One", * FROM table1;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜