postgres INTERSECT 中的类型不匹配
Type mismatch in postgres INTERSECT
我正在尝试执行 select 语句的交集,该语句检索 text[] 并将其与我动态计算的 text[] 进行交集。
当我 运行 这个脚本时,我得到错误
ERROR: INTERSECT types text[] and text cannot be matched
如何修复此错误?
do $$
declare
p json;
total_time float;
t float;
arr text[];
query1 text;
arr := '{}';
for j in 80..120 loop
arr := array_append(arr, j::text);
query1 := 'select sub_id from table where main_id=1 INTERSECT select unnest()';
execute 'explain (analyse, format json) ' || query1 using arr into p;
t := (p->0->>'Planning Time')::float + (p->0->>'Execution Time')::float;
total_time := total_time + t;
end loop;
我的架构table:
db=# \d+ table
Table "table"
Column | Type | Modifiers | Storage | Stats target | Description
---------------+---------+-----------+----------+--------------+-------------
main_id | integer | | plain | |
sub_id | text[] | | extended | |
sub_id
是一个数组。 arr
是一个数组,但您正在取消嵌套它 - 所以现在它是(文本)值的序列。您正在尝试对数组和文本值执行 INTERSECT。要修复它,要么同时取消嵌套,要么都不取消嵌套。 (但要选择正确的一个,因为每个结果都是不同的 - 您要么在数组本身上相交,要么在数组中包含的值上相交。)
SELECT UNNEST(sub_id) FROM table WHERE main_id = 1
INTERSECT
SELECT UNNEST(arr)
我正在尝试执行 select 语句的交集,该语句检索 text[] 并将其与我动态计算的 text[] 进行交集。
当我 运行 这个脚本时,我得到错误
ERROR: INTERSECT types text[] and text cannot be matched
如何修复此错误?
do $$
declare
p json;
total_time float;
t float;
arr text[];
query1 text;
arr := '{}';
for j in 80..120 loop
arr := array_append(arr, j::text);
query1 := 'select sub_id from table where main_id=1 INTERSECT select unnest()';
execute 'explain (analyse, format json) ' || query1 using arr into p;
t := (p->0->>'Planning Time')::float + (p->0->>'Execution Time')::float;
total_time := total_time + t;
end loop;
我的架构table:
db=# \d+ table
Table "table"
Column | Type | Modifiers | Storage | Stats target | Description
---------------+---------+-----------+----------+--------------+-------------
main_id | integer | | plain | |
sub_id | text[] | | extended | |
sub_id
是一个数组。 arr
是一个数组,但您正在取消嵌套它 - 所以现在它是(文本)值的序列。您正在尝试对数组和文本值执行 INTERSECT。要修复它,要么同时取消嵌套,要么都不取消嵌套。 (但要选择正确的一个,因为每个结果都是不同的 - 您要么在数组本身上相交,要么在数组中包含的值上相交。)
SELECT UNNEST(sub_id) FROM table WHERE main_id = 1
INTERSECT
SELECT UNNEST(arr)