Postgresql:使用 SELECT 和值插入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19800130/
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-10-21 01:10:36  来源:igfitidea点击:

Postgresql: INSERT INTO using SELECT and values

sqlpostgresql

提问by Emiel Steerneman

Postgresql version: 9.1.9

PostgreSQL 版本:9.1.9

I'm trying to insert data into a table using SELECT and passing values

我正在尝试使用 SELECT 和传递值将数据插入表中

Example of what i try to accomplish:

我尝试完成的示例:

current database:

当前数据库:

MyTable
character    number
'a'            '0'
'b'            '1'
'c'            '1'

what i want:

我想要的是:

MyTable
character    number
'a'            '0'
'b'            '1'
'c'            '1'
'b'            '2'
'c'            '2'

I tried different things but i can't seem to get it right. For example:

我尝试了不同的东西,但我似乎无法做对。例如:

INSERT INTO MyTable (character, number)
    (SELECT character FROM MyTable WHERE number = '1', '2')

Note: The numbers are actually long strings and therefore have to be in brackets.

注意:数字实际上是长字符串,因此必须放在括号中。

Thanks.

谢谢。

回答by pobrelkey

INSERT INTO MyTable (character, number) 
    SELECT character, '2' FROM MyTable WHERE number = '1'

to save the data in the same format on your example

在您的示例中以相同的格式保存数据