代码之家  ›  专栏  ›  技术社区  ›  Kushal Jain

如何在postgresql中创建具有值和空列的临时表

  •  1
  • Kushal Jain  · 技术社区  · 9 年前

    我对postgresql非常陌生。我想创建一个包含一些值和空列的临时表。这是我的查询,但它没有执行,但在 , (逗号)。

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

    我做错了什么?

    如何创建一个临时表,其中一些列使用select查询具有值,其他列为空?

    3 回复  |  直到 9 年前
        1
  •  8
  •   a_horse_with_no_name    9 年前

    只需选择空值:

    CREATE TEMP TABLE temp1 
    AS
    SELECT distinct region_name, country_name, null::integer as "count"
    from opens 
    where track_id=42;
    

    转换为整数( null::integer 42 as "count" 相反

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

    SELECT 声明 CREATE TABLE AS SELECT 在括号之间。

        2
  •  1
  •   TLR    9 年前

    这应该是有效的:

    CREATE TEMP TABLE temp1 AS 
    (SELECT distinct region_name, 
            country_name,
            0 as count 
     FROM   opens 
     WHERE track_id=42)
    
        3
  •  1
  •   Mr. Bhosale    9 年前

    试试这个。

                CREATE TEMP TABLE temp1 AS 
                (SELECT distinct region_name, 
                        country_name,
                        cast( '0' as integer) as count
                 FROM   opens 
                 WHERE track_id=42);