Postgresql:获取具有相似列值的记录

时间:2016-12-28 10:17:48

标签: postgresql

Table A

id   name   keywords
1    Obj1   a,b,c,austin black
2    Obj2   e,f,austin black,h
3    Obj3   k,l,m,n
4    Obj4   austin black,t,u,s
5    Obj5   z,r,q,w

我需要获取包含相似类型关键字的记录。因此,表格的结果必须是:

Records:
1,2,4

由于记录 1,2,4 是某个或其他关键字与至少任何其他关键字匹配的记录。

1 个答案:

答案 0 :(得分:2)

您可以将“csv”转换为数组,然后使用Postgres的数组函数:

select *
from the_table t1
where exists (select *
              from the_table t2
              where string_to_array(t1.keywords, ',') && string_to_array(t2.keywords, ',')
              and t1.id <> t2.id);
相关问题