2015-03-30 83 views
1

我想從我的數據庫中選擇所有行,其中一行至少包含一組詞/數組中的兩個詞。PostgreSQL根據數組值選擇行

作爲一個例子: 我有以下的數組:

'{"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[]) 

所以最後一行需要以某種方式adjustested,但我不知道如何 - 也許有一個內部連接?

我的單詞也存儲在一個不同的表中唯一的ID。

我的一位朋友推薦爲每一行獲取一個計數,但是我沒有在原始表中添加額外列的寫入權限。

背景:

我儲存我的tweets在Postgres數據庫和我申請的數據集LDA(隱含狄利克雷分配)。現在我得到了生成的主題和與每個主題相關的單詞(20個主題和25個單詞)。

+0

@mu太短 ID = _integer_ – user3815852 2015-03-30 23:26:00

+0

@mu太短數據庫的 標準的公共架構...鳴叫** ID = _integer_ 用戶id = _bigint_的 **結構 用戶名= _text_ tweetcontent_raw = _text_ tweetcontent = _text(梗鳴叫) tweetdate = _timestamp隨時間zone_ the_geom = _geometry_ 哪裏的話都存儲在表(results_lda): OID topic_id = _integer_ 字= _text_ topic_probability = _double precision_ – user3815852 2015-03-30 23:35:12

+0

SRY我的會議得到了中斷 – user3815852 2015-03-30 23:35:26

回答

0
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 
+0

非常感謝!這解決了問題:)我加了 'SELECT DISTINCT ON(tweet.id)tweet.id,tweet.content ....' 否則有重複的條目...非常感謝:) – user3815852 2015-03-31 11:23:34