我有一个名为“exclude”的表,其中包含以下标签:

-------------
id | tag
-------------
1    #oxford
2    #uk
3    #england
-------------

我还有一张叫“post”的桌子:
-----------------------------------------------
id | tags               | text
1    #oxford #funtimes    Sitting in the sun
2    #oz                  Beach fun
3    #england             Milk was a bad choice
-----------------------------------------------

为了对posts标记进行文本搜索,我运行了如下查询:
SELECT * FROM post WHERE to_tsvector(tags) @@ plainto_tsquery('mysearchterm')

但是,我现在希望能够排除所有的帖子,其中一些或所有的标签都在我的排除表中。在SQL/Postgres中有什么简单的方法可以做到这一点吗?
我尝试将标记行转换为一列,并在plainto tsquery函数中使用此术语,但它不起作用(我不知道如何执行文本搜索“不等于”,因此逻辑实际上是错误的,尽管在我的脑海中是正确的):
select * from post where to_tsvector(tags) @@ plainto_tsquery(
   select array_to_string(array(select RTRIM(value) from exclude where key = 'tag'), ' ')
)

最佳答案

你在玩什么版本的PostgreSQL?你的模式设计有多灵活?换句话说,你能随意改变吗?还是你无法控制?
当我读到你的问题时,立刻想到两件事。一个是您应该能够使用array和@>(contains)或这里是documentation
其次,您可以使用hstore并执行类似的操作。
致:

hstore @> hstore

它不是真正的hstore,因为您没有使用真正的key=>值对。但是,我想您可以做{tagname}=True或{tagname}=NULL。可能有点老套。
您可以看到文档(对于PostgreSQL 9.1)hstore以及如何使用它here

10-06 05:51