问题描述
我有两个表tab1和tab2.
I have two tables, tab1 and tab2.
tab2具有tab1的所有列,但带有用于时间戳记的额外列.我想做的是将所有行从tab1复制到tab2,并为我插入的所有行输入相同的timestamp时间戳列.我可以得到并输入正确的时间,但是我对如何复制和插入数据以及为我插入的所有行的时间戳填写最后一列感到困惑.
tab2 has all of the columns of tab1 but with an extra column for a timestamp. What I want to do is copy all of the rows from tab1 into tab2 and input the same time thae timestamp column for all rows that I insert. I can get and input the time fine, but I'm confused how to copy and insert data and fill in the last column with the timestamp for all of the rows that I inserted.
所以我想做
Insert into tab2 select * from tab1
但是我也想为tab2中的最后一列添加数据,我该怎么做?
but I also want to add data for that final column in tab2, how can I do this?
推荐答案
您可以将时间戳添加到选择列表中,以便两个表的列列表都匹配:
You could add the timestamp to the select list so the column lists of both tables would match:
INSERT INTO tab2
SELECT *, CURRENT_TIMESTAMP()
FROM tab1
编辑
要回答评论中的问题-您不必使用 CURRENT_TIMESTAMP()
.任何返回时间戳的表达式都可以.您可以使用硬编码的时间戳记:
EDIT
To answer the question in the comment - you don't have to use CURRENT_TIMESTAMP()
. Any expression returning a timestamp would do. You could use a hard-coded timestamp:
INSERT INTO tab2
SELECT *, TIMESTAMP('2017-07-07 19:43:00')
FROM tab1
这篇关于MySQL:将表复制到具有额外列的另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!