简体   繁体   English

如何在插入时选择带有子查询的单个元素并使用给定的返回 ID 更新同一个表?

[英]How to select a single element with subquery while insertion and update the same table with given returned Id?

I have a table, let's say, tableList我有一张桌子,比如说,tableList

unique_id唯一身份 next_id next_id randomCol1随机Col1
4 4 5 5 string 1字符串 1
5 5 6 6 string 2字符串 2
6 6 0 0 string 3字符串 3

Where unique_id is auto increment column and next_id is the unique_id of the row it is pointing to.其中 unique_id 是自增列, next_id 是它所指向的行的 unique_id。 At present we have 4->5->6->0(0 is a Non existing row).目前我们有 4->5->6->0(0 是不存在的行)。 Now I want to do an insertion of a row next to a given unique_Id, Let's say next to unique_Id=4.现在我想在给定的 unique_Id 旁边插入一行,假设在 unique_Id=4 旁边。
At the time of insertion I want to extract the next_id(5) of the given unique_id(here, 4).在插入时,我想提取给定 unique_id(此处为 4)的 next_id(5)。 The row which I'm about to insert will have next_id = 5 and row with unique_id = 4 will update its next_id to 7 (or whatever unique_id was generated at the time of insertion) So, My table will look like : 4->7->5->6->0我要插入的行将具有 next_id = 5 并且具有 unique_id = 4 的行将其 next_id 更新为 7(或在插入时生成的任何 unique_id)所以,我的表将如下所示:4->7 ->5->6->0

unique_id唯一身份 next_id next_id randomCol1随机Col1
4 4 7 7 string 1字符串 1
5 5 6 6 string 2字符串 2
6 6 0 0 string 3字符串 3
7 7 5 5 string 4字符串 4

I tried :我试过了 :

Insert into tableList(randomCol1,next_id)
values('string 4', select next_id from tableList where unique_id=4) returning unique_id as current_ID 
Update tableList set next_id = current_id where unique_id=4

Explanation : I'm trying to insert randomCol1 and next_id where next_id needs to be selected from given unique_id.解释:我正在尝试在需要从给定的 unique_id 中选择 next_id 的地方插入 randomCol1 和 next_id。 Hence, a sub query for that.因此,有一个子查询。 I want to update the next_id of given unique_id with whatever unique_id was returned after insertion in same query.我想用插入同一查询后返回的任何 unique_id 更新给定 unique_id 的 next_id。 But My subquery and update in the same query with returning id is not working.但是我的子查询和在返回 id 的同一查询中的更新不起作用。

demo:db<>fiddle 演示:db<>小提琴

You can use a chain of WITH clauses:您可以使用WITH子句链:

WITH select_part AS (       -- 1
    SELECT next_id
    FROM tableList
    WHERE unique_id = 4
),
insert_part AS (            -- 2
    INSERT INTO tableList ("next_id", "randomCol1")
        SELECT next_id, 'my new string'
        FROM select_part
    RETURNING unique_id
)
UPDATE tableList t          -- 3
SET next_id = i.unique_id
FROM (
    SELECT * 
    FROM insert_part
) i
WHERE t.unique_id = 4
  1. SELECT the next_id value from the row with unique_id = 4unique_id = 4的行中SELECT next_id
  2. Use this value in the INSERT statement and return the new generated id.INSERT语句中使用该值并返回新生成的 id。
  3. Use the new id to UPDATE the original row.使用新 id UPDATE原始行。

However, you might think about a separate function to do this.但是,您可能会考虑使用单独的函数来执行此操作。

demo:db<>fiddle 演示:db<>小提琴

CREATE OR REPLACE FUNCTION insert_value (
    _unique_id int, 
    _my_string text
) RETURNS void
AS $$
DECLARE 
    _next_val int;
    _new_id int;
BEGIN
    SELECT
        next_id
    FROM tableList
    WHERE unique_id = _unique_id
    INTO _next_val;
        
    INSERT INTO tableList("next_id", "randomCol1")
    VALUES (_next_val, _my_string)
    RETURNING unique_id
    INTO _new_id;
    
    UPDATE tableList t
    SET next_id = _new_id
    WHERE unique_id = _unique_id;
END;
$$ LANGUAGE plpgsql

Call it with:调用它:

SELECT insert_value(4, 'my_string');

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

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