1
说我有如下的表:Postgres的 - 插入和复合外键
a: integer
b: integer
c: integer
d: integer
code: text
(a, b) is a foreign key to another table 1
(c, d) is a foreign key to another table 2
插入很简单:
INSERT INTO table(a, b, c, d, code) VALUES(x1, y1, x2, y2, 'my code')
现在,我想插入而获取的值我的复合外键a,b
和c,d
在子查询中。类似这样的:
INSERT INTO table(a, b, c, d, code) VALUES
((SELECT a, b FROM other-table-1 WHERE ...),
(SELECT c, d FROM other-table-2 WHERE ...), 'my code')
上面的查询不起作用,但它说明了我试图实现的目标。
另一种尝试,但还没有工作(子查询必须返回一个列):
INSERT INTO table(a, b, code)
SELECT (SELECT a, b FROM other-table-1 WHERE ...),
(SELECT c, d FROM other-table-2 WHERE ...), 'my code')
这在某种程度上可能吗?
CTE是新的对我来说,我会尝试一下!谢谢。 –
如果你有样本日期,那么它将会是更全面的解决方案将在那里 – HaveNoDisplayName
我会用有意义的数据来尝试它。如果我偶然发现问题,我会提出一个新问题。您指向CTE的指示在这里帮了我很多,谢谢! –