如何基于SQL中的另一个列值将列添加到表

时间:2019-07-02 12:25:07

标签: mysql sql

我在数据库中有一个看起来像这样的表:

#table_tstamp
id, tstamp
1, 12.11
2, 14.0
3, 18.22
4, 24.34

我想在此表的另一列上遍历tstamp中的值,以使新值= {tstamp(i)* 2 + 5},以使更新后的表看起来像

#table_tstamp
id, tstamp, ts_new
1, 12.11, 29.22
2, 14.0 , 33.0
3, 18.22, 41.44
4, 24.34, 53.68

我尝试了这段代码,但对我不起作用(尽管创建了fetch_ts列,但具有NULL值):

ALTER TABLE table_tstamp
ADD fetch_ts float; 

insert into table_tstamp 
   SELECT 2*t2.tstamp+5 FROM table_tstamp t2;

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

1 个答案:

答案 0 :(得分:3)

您应该使用UPDATE而不是INSERT

ALTER TABLE table_tstamp ADD fetch_ts float; 

UPDATE table_tstamp SET fetch_ts = 2*tstamp+5