遍布每一行,直到postgresql中一个2D整数的末端

发布于 2025-01-23 04:15:19 字数 666 浏览 0 评论 0 原文

因此,基本上我在桌子里有一个2D阵列。 表SQL

CREATE TABLE IF NOT EXISTS public.table_name
(
"Person_Id" numeric(24,0) NOT NULL,
"Items" integer[],
);

因此,如果我这样做,

select "Items"[:][:] 
from public.table_name;

添加的所有元素

将获得我手动 ,我尝试了这个,

DO
$do$
BEGIN 
FOR i IN 1..500 LOOP
    update public.table_name
    if("Items"[i][1] == null)
        then exit;
    end if;
    set "Items"[i][1] = 41
    where "Items"[i][1] = 6;
END LOOP;
END
$do$;

我不希望这样,但是我想要的是循环在最后完成时停止。对于Person_ID 1,我需要执行3次循环以获取所有元素,但我知道,但是对于每个人,它并不总是3,它可能会有所不同,因此我如何限制循环,以免它运行不仅仅是需要的。请帮我 仅供参考,上述代码带有if子句给我一个错误。

so basically I got a 2d array in a table.
table SQL

CREATE TABLE IF NOT EXISTS public.table_name
(
"Person_Id" numeric(24,0) NOT NULL,
"Items" integer[],
);

so if I do this

select "Items"[:][:] 
from public.table_name;

I would get all the elements that I added manually

enter image description here

I put those values manually, and I tried this

DO
$do$
BEGIN 
FOR i IN 1..500 LOOP
    update public.table_name
    if("Items"[i][1] == null)
        then exit;
    end if;
    set "Items"[i][1] = 41
    where "Items"[i][1] = 6;
END LOOP;
END
$do$;

I don't want it like this but what I want is the loop to stop when my Items are done at the end. for the person_id 1, I need to do run the loop 3 times to get all elements and I know that but for each person, it's not always 3, it can vary, so how can I limit the loop so that it doesn't run more than what's needed. please help me
FYI, the above code with the if clause gives me an error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛刀软妹 2025-01-30 04:15:19

如果您必须在SQL中循环,则可能有更好的方法。

它不是数组,而是作为联接表更容易,更快。他们还强制执行引用完整性,这意味着您无法参考不存在的项目。联接表是传统上存储在SQL中的列表的方式。

假设 {{{1,4},{2,4},{3,4}} 意味着他们在项目2的项目1、4中有4个,以及项目3的4个...

create table people (
  id bigserial primary key
);

create table items (
  id bigserial primary key
);

-- The join table for the items each person has.
create table peoples_items (
  item_id bigint not null references items(id),
  person_id bigint not null references people(id),
  quantity int not null,

  -- A person can only one record per item.
  unique(item_id, person_id)
);

如果我“了解您的更新,您想将项目41的任何实例更改为项目6。您可以在一次更新中执行此操作。

update peoples_items
set item_id = 41
where item_id = 6;

everdation

其他注释:

  • 如果不需要,请不要引用列名,它会迫使您使用确切的情况。
  • 使用简单的 BigSerial 用于主键。如果有某种特定于业务的密钥,则可以单独使用该列。

If you have to loop in SQL, there's probably a better way.

Instead of an array, this is much easier and faster as a join table. They also enforce referential integrity, meaning you can't refer to an item which does not exist. Join tables are how lists are traditionally stored in SQL.

Assuming {{1,4},{2,4},{3,4}} means they have 4 of item 1, 4 of item 2, and 4 of item 3...

create table people (
  id bigserial primary key
);

create table items (
  id bigserial primary key
);

-- The join table for the items each person has.
create table peoples_items (
  item_id bigint not null references items(id),
  person_id bigint not null references people(id),
  quantity int not null,

  -- A person can only one record per item.
  unique(item_id, person_id)
);

If I'm understanding your update, you want to change any instance of item 41 to item 6. You can do that in a single update.

update peoples_items
set item_id = 41
where item_id = 6;

Demonstration.

Other notes:

  • Don't quote column names if you don't have to, it forces you to use the exact case.
  • Use a simple bigserial for primary keys. If there's some sort of business specific key make that a separate column.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文