Postgres - CREATE TABLE FROM SELECT

前端 未结 2 733
庸人自扰
庸人自扰 2020-12-14 07:52

I have two tables, one contains a large list of IDs and Info regarding those ids.

I have a second table Graph which just has two columns, each column co

相关标签:
2条回答
  • 2020-12-14 08:18

    It's as easy as:

    create table new_table
    as 
    select t1.col1, t2.col2
    from some_table t1
       join t2 on t1.id = t2.some_id;
    

    You can use any select statement for that. The column names of the new table are defined by the column aliases used in th query.

    More details in the manual: http://www.postgresql.org/docs/current/static/sql-createtableas.html

    0 讨论(0)
  • 2020-12-14 08:18

    You can create TEMP table if you need those small table only for that session. you can use below query to do that.

      DROP TABLE IF EXISTS temp_table;
        CREATE TEMP TABLE temp_table AS
         SELECT 
           i.id as info_id, i.information as information
         FROM
          info i
          INNER JOIN graph g ON i.id = g.id;
    

    Now you can use this temp_table for your next table in the function.

                        OR 
    

    you can also create table like below (if you not want to create it as TEMP):

    CREATE TABLE temp_table AS
         SELECT 
           i.id as info_id, i.information as information
         FROM
          info i
          INNER JOIN graph g ON i.id = g.id;
    
    0 讨论(0)
提交回复
热议问题