2014-05-14 40 views
0

我正在嘗試使用Postgres直接使用Postgres而不是某些命令語言擴展來查找帶有彼此雙目詞的句子。我的表是:如何引用子查詢中的當前搜索結果

TABLE word (
    spelling text, 
    wordid serial 
) 
TABLE sentence (
    sentenceid serial 
) 
TABLE item (
    sentenceid integer, 
    position smallint, 
    wordid integer 
) 

我有一個簡單的查詢,以找到一個詞的句子:通過其相應的項目都有一個項目裏有些字

SELECT DISTINCT sentence.sentenceid 
FROM item, word, sentence 
WHERE 
    word.spelling = 'word1' AND 
    item.wordid = word.wordid AND 
    sentence.sentenceid = item.sentenceid 

和我要的是過濾,反過來.sentenceid =當前查詢結果的項目(或句子).sentenceid和 item.position =當前查詢結果的item.position + 1

我沒有看到ALIAS的工作,也不知道如何引用子查詢中查詢的當前狀態。

+0

您可以添加一些示例數據和預期的輸出嗎?我很難理解「當前查詢結果的位置」和「當前查詢結果的位置+1」是什麼意思。它聽起來像是在尋找'lead()'或'lag()'函數 –

回答

0
select distinct sentenceid 
from 
    item i 
    inner join 
    item i2 using (sentenceid) 
    inner join 
    word w on w.wordid = i.wordid 
    inner join 
    word w2 on w2.wordid = i2.wordid 
where 
    w.spelling = 'word1' and 
    w2.spelling = 'word2' and 
    i.position = i2.position - 1