如何在postgresql中使用值和空列创建临时表

Kus*_*ain 1 sql postgresql temp-tables

我对postgresql很新.我想创建一个包含一些值和空列的临时表.这是我的查询,但它没有执行,但在,(逗号)给出错误.

CREATE TEMP TABLE temp1 
AS (
 SELECT distinct region_name, country_name 
 from opens 
 where track_id=42, count int)
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

如何使用选择查询和其他列为空的某些列创建临时表?

a_h*_*ame 5

只需选择一个NULL值:

CREATE TEMP TABLE temp1 
AS
SELECT distinct region_name, country_name, null::integer as "count"
from opens 
where track_id=42;
Run Code Online (Sandbox Code Playgroud)

强制转换为整数(null::integer)是必要的,否则Postgres将不知道要用于附加列的数据类型.如果你要提供一个不同的值,你当然可以使用例如42 as "count"代替

请注意,这count是一个保留关键字,因此如果要将其用作标识符,则必须使用双引号.然而,找到一个不同的名称会更好.

也没有必要在括号之间加上SELECT语句CREATE TABLE AS SELECT.