【发布时间】:2015-11-08 10:52:03
【问题描述】:
假设我有一个包含以下列的表格:
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')
这有可能吗?
【问题讨论】:
标签: sql postgresql foreign-keys primary-key composite-key