以下两句:
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);
如果句子中有两个词,那只会给我一个结果。我不确定该怎么做这并不复杂。