遍布每一行,直到postgresql中一个2D整数的末端
因此,基本上我在桌子里有一个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子句给我一个错误。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您必须在SQL中循环,则可能有更好的方法。
它不是数组,而是作为联接表更容易,更快。他们还强制执行引用完整性,这意味着您无法参考不存在的项目。联接表是传统上存储在SQL中的列表的方式。
假设
{{{1,4},{2,4},{3,4}}
意味着他们在项目2的项目1、4中有4个,以及项目3的4个...如果我“了解您的更新,您想将项目41的任何实例更改为项目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...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.
Demonstration.
Other notes:
bigserial
for primary keys. If there's some sort of business specific key make that a separate column.