我需要将 2 条记录插入到 2 个不同的表中。问题是这两条记录将具有相同的 ID。 例如:
我有我的 Mannto 表,其中包含 IdMan 和 oters 字段。我还有我的服务表及其 IdServ。
我该怎么做才能使这一个相等?我正在使用 Postgre。 Mannto 表的 ID 是序列号,我需要将其用作服务表中的外键
我尝试了以下方法,但它不起作用:
Insert into Mannto ( idMan, field 1 field2 ...etc)
values ( default, 'f1', 'f2'...etc)
Insert into Service ( idServ, fkMannto, field1...etc)
values (default, (in this part I need the same ManntoId called idMan), 'f1')
感谢您提供的任何帮助!
最佳答案
INSERT INTO Mannto ( field1, field2 ...etc) VALUES ( 'f1', 'f2'...etc)
RETURNING idMan;
http://www.postgresql.org/docs/current/static/sql-insert.html
关于sql - 我需要在不同的表中插入 2 条记录,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7014355/