10

以下两句:

hello there
bye!

在表 sentence_words 中表示为:

WORD_ID  SENTENCE_ID    WORD    WORD_NUMBER
10       1              hello   1
11       1              there   2
12       2              bye!    1

我想做一个给我结果的外连接查询:

WORD1      WORD2
hello      there
bye!       NULL

请注意,我可能想从句子中间开始,所以我不能假设 word2 的 word_number = 2。如果我选​​择 my_start_number = 2,那么查询应该给我:

WORD1   WORD2
there   NULL

我试过:

(my_start_number = 1)

select  s1.word word1, s2.word word2
from sentence_words s1
left join sentence_words s2
on s1.sentence_id = s2.sentence_id
where s1.word_number = my_start_number
 and (s2.word_number = s1.word_number +1 or s2.word_number is null);

如果句子中有两个词,那只会给我一个结果。我不确定该怎么做这并不复杂。

4

2 回答 2

12

word_number + 1需求移入LEFT JOIN.

SELECT
  s1.word word1, s2.word word2
FROM
  sentence_words s1
LEFT JOIN
  sentence_words s2
    ON  s2.sentence_id = s1.sentence_id
    AND s2.word_number = s1.word_number + 1
WHERE
  s1.word_number = my_start_number

死神编辑:

虽然上面修复了 LEFT JOIN 的使用,但我建议根本不要使用连接......

SELECT
  sentence_id,
  MAX(CASE WHEN pos = 0 THEN word END)   AS word1,
  MAX(CASE WHEN pos = 1 THEN word END)   AS word2
FROM
(
  SELECT
    sentence_id,
    word_number - MY_START_NUMBER   AS pos,
    word
  FROM
    sentence_words
)
  AS offset_sentence_words
WHERE
  pos IN (0, 1)
GROUP BY
  sentence_id
于 2012-04-26T14:01:10.910 回答
1

Dems的答案绝对是正确的。我决定写这个答案来解释你原来的解决方案不起作用的原因。这是因为您正在尝试过滤左外连接的以下结果集(显示所有列,一些名称被缩写以适应):

s1.WORD_ID s1.SENT_ID s1.WORD  s1.WORD_NUM s2.WORD_ID s2.SENT_ID s2.WORD  s2.WORD_NUM
10         1          hello    1           10         1          hello    1
10         1          hello    1           11         1          there    2
11         1          there    2           10         1          hello    1
11         1          there    2           11         1          there    2
12         2          bye!     1           12         2          bye!     1

现在,看看你的 where 子句:

where s1.word_number = my_start_number  
 and (s2.word_number = s1.word_number +1 or s2.word_number is null);  

...而且应该相对容易看出它为什么不起作用。例如,s2.word_number从不NULL

于 2012-04-26T14:12:25.210 回答