【发布时间】:2021-12-19 12:57:00
【问题描述】:
我有两列 ind 和 tar 都包含数组。
ind tar
{10} {10}
{6} {5,6}
{4,5,6} {5,6}
{5,6} {5,6}
{7,8} {11}
{11} {5,6,7}
{11} {8}
{9,10} {6}
我想查找两个数组中是否存在一个值,如果是,我只想将其保留在列ind。例如,在第一行,我在两列中都有值 10。我只想在ind 列中得到这个值,并将tar 列留空。这是预期的结果:
ind tar
{10}
{6} {5}
{4,5,6}
{5,6}
{7,8} {11}
{11} {5,6,7}
{11} {8}
{9,10} {6}
如何在 PostgreSQL 中做到这一点?
到目前为止,我只设法找到了公共元素,但我不知道如何继续将它们仅保留在 ind 列中并将它们从 tar 列中删除。
with t1 as (
select distinct ind, tar
from table_1
join table_2 using (id)
limit 50
),
t2 as (
select ind & tar as common_el, ind , tar
from t1
)
select *
from t2
结果如下:
common_el ind tar
{10} {10} {10}
{6} {6} {5,6}
{5,6} {4,5,6} {5,6}
{5,6} {5,6} {5,6}
【问题讨论】:
标签: sql arrays postgresql