代码之家  ›  专栏  ›  技术社区  ›  hksfho

如何使用for循环函数在postgresql中插入数据?

  •  1
  • hksfho  · 技术社区  · 7 年前

    我有一个数组字符串[1,2,3,4]

    这是我的sql

    INSERT INTO account(account_id, parent_id) VALUES (1, 10);
    INSERT INTO account(account_id, parent_id) VALUES (2, 10);
    INSERT INTO account(account_id, parent_id) VALUES (3, 10);
    INSERT INTO account(account_id, parent_id) VALUES (4, 10);
    

    如何使用postgresql forloop函数插入?

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  2
  •   a_horse_with_no_name    7 年前

    无需循环:

    insert into account(account_id, parent_id)
    select t.id, 10
    from unnest(array[1,2,3,4]) as t(id);