简体   繁体   English

psql-将条目从另一个表插入到一个表中

[英]psql - Inserting entries into a table from another table

How to insert entries into table foo only the comm_id from table waldo while the other columns (managed, status, and type) are added in static values? 如何仅将表waldo中的comm_id插入表foo中 ,而将其他列(托管,状态和类型)添加为静态值?

TABLE foo 表foo

     comm_id     |    managed    |    status    | type |
-----------------+---------------+--------------+------+
  COMM_11.21.6   |     true      |     NULL     | NULL |
  COMM_14.15.7   |     true      |     NULL     | NULL |
  COMM_13.03.9   |     true      |     NULL     | NULL |

TABLE waldo 表瓦尔多

     comm_id     |    address    |  stat_id  |
-----------------+---------------+-----------+
  COMM_10.10.6   |     12345     |     1     |
  COMM_14.15.7   |     78543     |     2     |

Desired Output for TABLE foo TABLE foo的所需输出

     comm_id     |    managed    |    status    | type |
-----------------+---------------+--------------+------+
  COMM_11.21.6   |     true      |     NULL     | NULL |
  COMM_14.15.7   |     true      |     NULL     | NULL |
  COMM_13.03.9   |     true      |     NULL     | NULL |
  COMM_10.10.6   |     true      |     NULL     | NULL |

the values managed = true, status = NULL and type = NULL is static so I just want to add them by value. 值托管=真,状态= NULL和类型= NULL是静态的,因此我只想按值添加它们。 And COMM_14.15.7 is not added to foo because the id already exists there. 并且COMM_14.15.7未添加到foo中,因为该id已经存在于foo中

INSERT INTO foo (comm_id, managed, status, type ) VALUES ('id from waldo', 'true', 'NULL', 'NULL' );

The query above inserts static values to the table. 上面的查询将静态值插入到表中。

INSERT INTO foo SELECT comm_id FROM waldo WHERE comm_id NOT IN (SELECT comm_id FROM foo);

Is there any way to manipulate or combine these queries so I can get all the comm_id from waldo into foo and put static values at the same time? 有没有什么方法可以处理或组合这些查询,以便我可以将所有wald的comm_id从waldo转换为foo并同时放置静态值? Your suggestions and ideas are highly appreciated. 非常感谢您的建议和想法。

is it 是吗

INSERT INTO foo (comm_id, managed, status, type ) 
select comm_id , true, NULL, NULL 
from waldo 
WHERE comm_id NOT IN (SELECT comm_id FROM foo);

note I omitted quotes around boolean true and nulls, which is only a guess not knowing the structure of foo 注意,我省略了布尔值true和null周围的引号,这只是一个猜测,不知道foo的结构

Try this: 尝试这个:

INSERT INTO foo (com_id,managed,status,type)
select comm_id,'true',NULL,NULL from
(
SELECT comm_id FROM waldo 
minus
SELECT comm_id FROM foo
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM