在 INSERT 上使用 WITH 时出现问题(“缺少 FROM 子句条目”)

Jeh*_*han 8 sql postgresql

我正在尝试使用以前的一些 CTE 的信息构建一个新行,但我似乎无法获得正确的语法。

BEGIN;
UPDATE users
SET balance = balance - 10
WHERE user_id = 1;

WITH te AS (
  SELECT accountInsertOrSelect('the.hound', 'farcebook', 'rong') AS account_id
), tr AS (
  SELECT account_id FROM accounts
  WHERE user_id = 1
  AND provider = 'farcebook'
  LIMIT 1
)
INSERT INTO tips (tipper_id, tippee_id, amount)
VALUES (te.account_id, tr.account_id, 10);
COMMIT;
Run Code Online (Sandbox Code Playgroud)

给出错误missing FROM-clause entry for table "te"

shr*_*t18 6

FROM CLAUSE当您尝试插入时,您需要指定 CTE ,如下所示:

INSERT INTO tips (tipper_id, tippee_id, amount) VALUES
((SELECT account_id FROM te), (SELECT account_id from tr), 10)
Run Code Online (Sandbox Code Playgroud)