在 MySQL 查询中使用 SELECT 执行 INSERT INTO 时如何添加静态值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9059310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 11:56:26  来源:igfitidea点击:

How to add static value when doing INSERT INTO with SELECT in a MySQL query?

mysqlsqldatabase

提问by JasonTS

I have two MySQL tables a and b with fields x and y. Table b has 1 extra field z. Table a is in database db1 and b is in db2. I want to copy x and y from a to b and set a static value for z. How can I do that ?

我有两个带有字段 x 和 y 的 MySQL 表 a 和 b。表 b 有 1 个额外的字段 z。表 a 在数据库 db1 中,b 在 db2 中。我想将 x 和 y 从 a 复制到 b 并为 z 设置一个静态值。我怎样才能做到这一点 ?

db1.a.x -> db2.b.x
db1.a.y -> db2.b.y
4 -> db2.b.z

So far I have:

到目前为止,我有:

"INSERT INTO db2.b (x,y) SELECT x,y FROM db1.a"

How do I set db2.b.z to 4 ? I do not want to set a permanent default variable for the table.

如何将 db2.bz 设置为 4 ?我不想为表设置一个永久的默认变量。

回答by xdazz

SELECT 4will give you 4, so try:

SELECT 4会给你4,所以试试:

INSERT INTO db2.b (x,y,z) SELECT x,y,4 FROM db1.a

回答by pilcrow

INSERT INTO db2.b (x, y, z) SELECT x, y, 4 FROM db1.a;