MySQL:将表复制到具有额外列的另一个表

编程入门 行业动态 更新时间:2024-10-17 15:21:47
本文介绍了MySQL:将表复制到具有额外列的另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个表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:将表复制到具有额外列的另一个表

本文发布于:2023-10-27 10:59:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!