【问题标题】:Postgresql: Get records having similar column valuesPostgresql:获取具有相似列值的记录
【发布时间】:2016-12-28 10:17:48
【问题描述】:
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 是其某些或其他关键字至少与任何其他关键字匹配的记录。

【问题讨论】:

  • 正如其他人可能会评论的那样,您为什么要以非规范化的 CSV 格式存储关键字数据?
  • @TimBiegeleisen 是的,我知道。但需要为其他一些依赖项存储它。对于上述问题,我考虑将一个函数写入数据库本身,该函数将遍历每个关键字并与其他关键字匹配。但这不是一个有效的解决方案。正在尝试任何可以通过解决关键字列来提供帮助的查询

标签: postgresql


【解决方案1】:

您可以将“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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多