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

psql - Inserting entries into a table from another table

如何仅将条目插入 table foo comm_id 来自 table waldo而其他列(托管、状态和类型)以静态值添加?

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 |

TABLE沃尔多

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

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 |

值 managed = true、status = NULL 和 type = NULL 是静态的,所以我只想按值添加它们。并且 COMM_14.15.7 没有添加到 foo 因为 id 已经存在了。

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

上面的查询将静态值插入 table。

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

有什么方法可以操纵或组合这些查询,以便我可以将所有 comm_id 从 waldo 获取到 foo 并同时放入静态值?非常感谢您的建议和想法。

试试这个:

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
);

是吗

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);

注意我省略了 boolean true 和 nulls 周围的引号,这只是一个猜测,不知道 foo

的结构