PostgreSQL 不接受带有 VALUES 和 FROM 的 INSERT?

PostgreSQL won't accept INSERT with VALUES and FROM?

我正在尝试获取从 SELECT 获得的值,然后 INSERT 该值与其他值一起进入另一个 table:

WITH data AS (SELECT name FROM programmes WHERE id = )
INSERT INTO purchases (name, other_details, some_more_stuff)
VALUES (data.name, , ) FROM data;

但是 PostgreSQL 给出 42601 ERROR: syntax error at or near "FROM" LINE 1: ...(data.name, , ) FROM data

INSERTdocumentation 没有在同一个查询中给出任何 VALUESFROM 的例子。这种查询的正确语法是什么?还是无法以这种方式表达我的查询?

使用insert . . . select:

WITH data AS (
      SELECT name
      FROM programmes 
      WHERE id = 
    )
INSERT INTO purchases (name, other_details, some_more_stuff) 
    SELECT data.name, , 
    FROM data;

不仅仅是 Postgres。这就是 SQL 的工作原理。老实说,我不认为学习 insert . . . values 有什么特别的用处,因为使用 insert . . . select 可以做任何事情甚至更多。 (好吧,除非您使用的是不允许 "constant" 子查询的 MS Access。)

您不需要常见的 table 表达式。

INSERT INTO purchases (name, other_details, some_more_stuff) 
SELECT name, , 
FROM programmes 
WHERE id = 

您尝试应用的语法不正确。您不能在 VALUES 列表中使用来自 table 或查询的列,因为该列表必须仅包含常量。来自 the documentation(强调已添加):

VALUES provides a way to generate a "constant table" that can be used in a query without having to actually create and populate a table on-disk.

另见 VALUES syntax.