postgresql 通过PGadmin4将不同表的不同查询结果插入Postgres中新创建的表中

nfs0ujit  于 2022-11-29  发布在  PostgreSQL
关注(0)|答案(1)|浏览(289)

因此,我创建了一个包含多个列的表来收集有关数据库的一些信息。

CREATE TABLE DATENBANKEN (

    ID serial,
    name VARCHAR(20),
    Erstellt timestamp,
    Datenbankgröße VARCHAR(20),
    "Collation" VARCHAR (20),
    Beschreibung VARCHAR (50) 
)

并且使用下面的插入语句,我能够用所需的信息填充行

INSERT INTO DATENBANKEN (id, name, Erstellt, Datenbankgröße, "Collation")
    SELECT pg_database.oid,
    pg_database.datname,
    (pg_stat_file('base/'||pg_database.oid ||'/PG_VERSION')).modification,
    pg_size_pretty(pg_database_size(datname)),
    pg_database.datcollate datcollate from pg_database

this is the results
以上所有值都是从表(pg_database)中捕获的
现在,最后一个值“Beschreibung”位于另一个名为(pg_shdescription)的表中
因此在本例中,我必须专门为列“Beschreibung”创建另一个插入语句

INSERT INTO DATENBANKEN (Beschreibung)
    select pg_shdescription.description from pg_shdescription

as you can see the rows in the column "Beschreibung" were not inserted beside the first three rows as i expected, but were added as additional rows with no connection to the data above.
this is the table pg_shdescription and as you can see, for every objoid there is a specific description. So 1 is "default template for new databases"
so here the 4th row in the column "Beschreibung" should have been in the second row where the datacenter name "template 1 is"
我在这里做错了什么?将不同表中的某些数据插入到仍然链接在一起的新表中的最佳方法是什么?
我真的很感谢你的帮助,任何帮助🙂
我在语句中尝试了INNER JOIN,但没有成功
第一个

w8f9ii69

w8f9ii691#

您应该在插入之前连接表,否则您需要UPDATE DATENbBANKEN and not INSERT INTO '

INSERT INTO DATENBANKEN (id, name, Erstellt, Datenbankgröße, "Collation",Beschreibung )
    SELECT pg_database.oid,
       pg_database.datname,
       (pg_stat_file('base/'||pg_database.oid ||'/PG_VERSION')).modification,
       pg_size_pretty(pg_database_size(datname)),
       pg_database.datcollate datcollate,
      pg_shdescription.description 
    from pg_database JOIN pg_shdescription
          ON pg_database.oid = pg_shdescription.objoid

相关问题