我想从我的数据库中选择所有行,其中一行至少包含一组单词/数组中的两个术语。
举个例子: 我有以下数组:
'{"test", "god", "safe", "name", "hello", "pray", "stay", "word", "peopl", "rain", "lord", "make", "life", "hope", "whatever", "makes", "strong", "stop", "give", "television"}'
我在数据库中存储了一个推文数据集。所以我想知道哪些推文(列名:tweet.content)包含至少 至少 两个 个词。
我当前的代码看起来像这样(当然它只选择了一个词......):
CREATE OR REPLACE VIEW tweet_selection AS
SELECT tweet.id, tweet.content, tweet.username, tweet.geometry,
FROM tweet
WHERE tweet.topic_indicator > 0.15::double precision
AND string_to_array(lower(tweet.content)) = ANY(SELECT '{"test", "god", "safe", "name", "hello", "pray", "stay", "word", "peopl", "rain", "lord", "make", "life", "hope", "whatever", "makes", "strong", "stop", "give", "television"}'::text[])
所以最后一行需要以某种方式进行调整,但我不知道如何 - 也许使用内部连接?!
我在另一个表中也存储了具有唯一 ID 的单词。
我的一个 friend 建议对每一行进行计数,但我没有在原始表中添加额外列的写入权限。
背景:
我将推文存储在 postgres 数据库中,并在数据集上应用了 LDA(潜在狄利克雷分配)。现在我得到了生成的主题和与每个主题相关的单词(20 个主题和 25 个单词)。
最佳答案
select DISTINCT ON (tweet.id) tweet.id, tweet.content, tweet.username, tweet.geometry
from tweet
where
tweet.topic_indicator > 0.15::double precision
and (
select count(distinct word)
from
unnest(
array['test', 'god', 'safe', 'name', 'hello', 'pray', 'stay', 'word', 'peopl', 'rain', 'lord', 'make', 'life', 'hope', 'whatever', 'makes', 'strong', 'stop', 'give', 'television']::text[]
) s(word)
inner join
regexp_split_to_table(lower(tweet.content), ' ') v (word) using (word)
) >= 2
关于sql - PostgreSQL 根据数组值的组合选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29357179/