数组中的 PostgreSQL 整数不可合并连接
PostgreSQL Integer in Array is not merge-joinable
我想在整数数组中的整数条件下执行 FULL OUTER JOIN。
查询看起来像这样:
SELECT thing FROM table1 t1
FULL OUTER JOIN table2 t2
ON t1.id = ANY(t2.array_of_ids)
但是我得到以下错误:
FULL JOIN is only supported with merge-joinable or hash-joinable join conditions
有没有办法使这个条件可合并连接?
嗯。 . .是这样的吗?
SELECT thing
FROM table1 t1 FULL OUTER JOIN
(table2 t2 CROSS JOIN
UNNEST(t2.array_of_ids) t2_id
)
ON t1.id = t2_id;
问题在于它会成倍增加行数。那么,构建 FULL JOIN
怎么样?
SELECT . . .
FROM table1 t1 LEFT JOIN
(table2 t2 CROSS JOIN
UNNEST(t2.array_of_ids) t2_id
)
ON t1.id = t2_id
UNION ALL
SELECT . . .
FROM table2 t2
WHERE NOT EXISTS (SELECT 1
FROM UNNEST(t2.array_of_ids) t2_id JOIN
table1 t1
ON t2_id = t1.id
);
我想在整数数组中的整数条件下执行 FULL OUTER JOIN。
查询看起来像这样:
SELECT thing FROM table1 t1
FULL OUTER JOIN table2 t2
ON t1.id = ANY(t2.array_of_ids)
但是我得到以下错误:
FULL JOIN is only supported with merge-joinable or hash-joinable join conditions
有没有办法使这个条件可合并连接?
嗯。 . .是这样的吗?
SELECT thing
FROM table1 t1 FULL OUTER JOIN
(table2 t2 CROSS JOIN
UNNEST(t2.array_of_ids) t2_id
)
ON t1.id = t2_id;
问题在于它会成倍增加行数。那么,构建 FULL JOIN
怎么样?
SELECT . . .
FROM table1 t1 LEFT JOIN
(table2 t2 CROSS JOIN
UNNEST(t2.array_of_ids) t2_id
)
ON t1.id = t2_id
UNION ALL
SELECT . . .
FROM table2 t2
WHERE NOT EXISTS (SELECT 1
FROM UNNEST(t2.array_of_ids) t2_id JOIN
table1 t1
ON t2_id = t1.id
);