简体   繁体   English

将插入结果插入另一个表

[英]Insert the result of an insert into another table

With PostgreSQL, given the following tables : 对于PostgreSQL,给出下表:

CREATE TABLE events (
  id BIGSERIAL PRIMARY KEY,
  timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
);

CREATE TABLE event_details (
  uuid UUID NOT NULL,
  signal_strength SMALLINT NOT NULL,
  event_id INTEGER REFERENCES events (id) ON DELETE RESTRICT,
  PRIMARY KEY (event_id)
);

To insert an event, I must insert the event details first, then insert the "generic" event after, linking with the event details. 要插入事件,我必须先插入事件详细信息,然后再插入“通用”事件,并与事件详细信息链接。 I have built this query to do this : 我已经建立了这个查询来做到这一点:

INSERT INTO event_details VALUES ('B9056867-459D-4F0B-8627-7456C880F54B', 50, (INSERT INTO events VALUES (DEFAULT, '2014-03-21 15:37:44.683081-04') RETURNING id));

But I get this error : ERROR: syntax error at or near "INTO" 但我收到此错误: 错误:“ INTO”或附近的语法错误

So I can't insert a value with an INSERT INTO like I would do with a SELECT . 所以我不能像SELECT那样使用INSERT INTO插入值。 How can I forward the newly created row id into the foreign key column of event_details? 如何将新创建的行ID转发到event_details的外键列中?

There may be a lot of these queries when the system will be running, so I would like to avoid retrieving the id back in my code (in NodeJS) and then do the second INSERT in a separate statement. 当系统将要运行时,可能会有很多这样的查询,因此我想避免在我的代码中(在NodeJS中)取回ID,然后在单独的语句中执行第二次INSERT

Thanks! 谢谢!

PS : I simplified the SQL of the tables, but I have some good reasons to separate event_details with events (there are many event types with different details). PS:我简化了表的SQL,但是我有一些充分的理由将event_details与事件分开(有许多事件类型具有不同的详细信息)。

[edit] [编辑]

The right way to do it, in this situation, is : 在这种情况下,正确的做法是:

WITH new_event AS (INSERT INTO events VALUES (DEFAULT, '2014-03-21 15:37:44.683081-04') RETURNING id) INSERT INTO event_details VALUES ('B9056867-459D-4F0B-8627-7456C880F54B', 50, (SELECT id FROM new_event LIMIT 1));

Thanks to Jaffar's answer for this solution! 感谢Jaffar对这个解决方案的回答

PostgreSQL has such an extended interpretation of the VALUES clause that it may be used as a subquery by itself. PostgreSQL对VALUES子句有扩展的解释,以至于它本身可以用作子查询。

So you may express your query in this form: 因此,您可以通过以下形式表达您的查询:

WITH new_invoice AS (
    INSERT INTO ...
    RETURNING id
),
v(a,b,c,d) AS (values
  ($27,$28,$29,$30),
  ($31,$32,$33,$34),
  ...
)
INSERT INTO invoiceItems (invoice_id, name, qty, price, description)
 SELECT new_invoice.id, a,b,c,d FROM v, new_invoice;

That assumes you want to insert the cartesian product of new_invoice and the values, which mostly makes sense if new_invoice is actually a single-row value. 假定您要插入new_invoice和值的笛卡尔积,这在new_invoice实际上是单行值的情况下最有意义。

You must separate the queries since an insert-subquery is not a valid value expression: 您必须分开查询,因为插入子查询不是有效的值表达式:

http://www.postgresql.org/docs/9.3/static/sql-expressions.html http://www.postgresql.org/docs/9.3/static/sql-expressions.html

Or you can make a stored procedure. 或者您可以制作一个存储过程。 That reduces the overhead. 这样可以减少开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM