我已经看到了使用此示例进行批量插入的好方法:
WITH p AS (
INSERT INTO parent_table (column_1)
SELECT $1
RETURNING id)
INSERT INTO child_table (parent_table_id, column_a)
SELECT p.id, a
FROM p, unnest($2::text[]) AS a
Run Code Online (Sandbox Code Playgroud)
但是,我需要从多个数组中插入多行,所以我尝试了以下语法:
WITH p AS (
INSERT INTO parent_table (column_1)
SELECT $1
RETURNING id)
INSERT INTO child_table (parent_table_id, column_a, column_b)
SELECT p.id, a, b
FROM p, unnest($2::text[]) AS a, unnest($3::bigint[]) AS b
Run Code Online (Sandbox Code Playgroud)
我在parent_table_idand上有一个主键column_a,当我尝试执行此查询时,Postgres 抱怨重复键冲突。
应该如何解开数组以便它们形成单独的行?
换句话说,如果$2和$3都有两个条目,那么第一个条目如何$2只插入第一个条目,$3并且对于各自的第二个条目都相同?
如果这是不可能的,我可以构造一个多维数组吗?如果是这样,它应该如何与多种数组类型一起传递,多维数组语法是什么?
根据主题,我有一个 varchar 字段,我在其中存储一些用逗号分隔的数字。该列表根据客户的选择增长和缩小。然后我在查询中使用它来检查特定值是否在该列表中
value || ','
Run Code Online (Sandbox Code Playgroud)
它运行良好,但正在考虑将其转换为整数数组。
这在技术/性能方式和正确编程数据库方面是否是更好的解决方案?
在 Postgres 9.4 中,我试图提取UNIQUEPostgres 中给定表的约束中涉及的所有列的名称。
看起来此类列的名称包含在pg_constraint. 根据文档,与我的问题相关的列称为conkey,它恰好是一个ints数组。
关键是,我提出的查询给了我错误的结果,我很确定这是因为我conkey以错误的方式加入。查询如下:
SELECT
pg_attribute.attname,
pg_constraint.*
FROM
pg_attribute
INNER JOIN pg_constraint
ON pg_attribute.attnum = ANY (pg_constraint.conkey)
WHERE pg_constraint.conrelid = (
SELECT
oid
FROM
pg_class
WHERE
relname LIKE 'test_table'
)
AND pg_constraint.contype = 'u'; -- to filter out non-unique constraints
Run Code Online (Sandbox Code Playgroud)
这是一个用于快速重现的表 DDL:
CREATE TABLE "test_table" (
"date" date DEFAULT now() NOT NULL,
"foo" varchar COLLATE "default" NOT NULL
CONSTRAINT "test_table_foo_key" UNIQUE ("foo")
) …Run Code Online (Sandbox Code Playgroud) 我在当前存储为 VARCHAR 的 Postgres 数据库中有一个非常基本的分类列。我可以选择每个计数:
我虽然添加一个ORDER BY array_position()会做到这一点:
SELECT color, count(*)
FROM research
GROUP BY color
ORDER BY array_position(ARRAY['Red','Orange','Yellow','Green','Blue'], color);
Run Code Online (Sandbox Code Playgroud)
但我看到一个类型错误:
ERROR: function array_position(text[], character varying) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 98
Run Code Online (Sandbox Code Playgroud)
我需要投射什么color才能使用该array_position功能对其进行排序?
我正在使用由脚本( Osm2pgsql )创建的 postgres 数据库。它创建了一些类型为_text的二维数组的字段text。我试图理解为什么它不只是创建一个text[][]字段,以及为什么这种类型存在于 postgres 中。
我可以在文档中找到这个链接,其中写着:
当您定义新的基本类型时,PostgreSQL 会自动提供对该类型数组的支持。数组类型通常与基类型具有相同的名称,并在前面添加下划线字符 (_)。
但我不明白为什么当文本是 postgres 的内置类型时它使用这个“下划线”类型名称?
是否可以通过索引删除 Postgres 数组元素?(使用 Postgres 9.3。)
我在文档(http://www.postgresql.org/docs/9.3/static/functions-array.html)中没有看到任何相关内容,但也许我还缺少其他功能?
是否可以FOREACH在 PL/pgSQL 中循环多个数组?就我而言,3 个具有相同维度的数组。我如何传递元素,如:
for(int i = 0 ; i < array1.length ; i++){
my_method(array1[i], array2[i], array3[i]);
}
Run Code Online (Sandbox Code Playgroud)
这是我的尝试:
select dugong.session_hugeInsert( 3, '5,2,3,1', '4,3,3,2');
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)NOTICE: The transaction is in an uncommitable state.Transaction was rolled back. NOTICE: query "SELECT Question_ID, UserResponseID_List, UserResponseList" returned 3 columns 42601 ERROR: cannot begin/end transactions in PL/pgSQL HINT: Use a BEGIN block with an EXCEPTION clause instead. CONTEXT: PL/pgSQL function dugong.session_hugeinsert(integer,text,text) line 61 at SQL statement
我的功能:
CREATE OR REPLACE FUNCTION Dugong.Session_hugeInsert(
Quiz_ID_ int, …Run Code Online (Sandbox Code Playgroud) 使用 Postgres 9.4,我有兴趣拥有一个整数数组,例如user_ids_who_like并提供一个用户数组(例如user_ids_i_am_following)来对该交集进行排序。
就像是:
select *
from items
where [there is an intersection between
user_ids_who_like with user_ids_i_am_following]
order by intersection(user_ids_who_like).count
Run Code Online (Sandbox Code Playgroud)
是否可以通过数组交集进行分组和排序?
示例数据:
items
name | user_ids_who_like
'birds' | '{1,3,5,8}'
'planes' | '{2,3,4,11}'
'spaceships' | '{3,4,6}'
Run Code Online (Sandbox Code Playgroud)
对于给定的user_ids_who_i_follow = [3,4,11],我可以执行以下操作:
select * from items
where <user_ids_who_like intersects with user_ids_who_i_follow>
order by <count of that intersection>
Run Code Online (Sandbox Code Playgroud)
想要的结果:
name | user_ids_who_like | count
'planes' | '{2,3,4,11}' | 3
'spaceships' | '{3,4,6}' | 2
'birds' | '{1,3,5,8}' | 1 …Run Code Online (Sandbox Code Playgroud) 是否有任何理由使用 JSONB 值数组而不仅仅是原始 JSONB?
我的用例是我需要在 JSON 对象列表中存储自定义表单定义。我的问题是将其存储为原始 JSON 值还是将其拆分为单独的 JSONB 对象会更好。
每种解决方案的优缺点是什么?
我有一组数组 [ID, TYPE]。
所有值都是整数。数组可以是integer[]或jsonb类型。
SELECT array[[442,2],[443,2]]
SELECT to_jsonb(array[[1,2],[3,4]])
Run Code Online (Sandbox Code Playgroud)
如何检查此数组是否包含数组 [443,2]?
编辑 这里描述的检查方法之一。
1# SELECT array[[442,2],[443,2]] @> array[443,2] -- returns True
2# SELECT array[[442,2],[443,2]] @> array[2,443] -- returns True too
Run Code Online (Sandbox Code Playgroud)
对于我的情况,我想得到 False 2#
array ×10
postgresql ×10
bulk-insert ×1
catalogs ×1
enum ×1
join ×1
json ×1
order-by ×1
plpgsql ×1