gpt4 book ai didi

sql - 从使用(WHERE)条件选择的行中获取上一行和下一行

转载 作者:行者123 更新时间:2023-12-03 07:29:18 24 4
gpt4 key购买 nike

例如,我有以下语句:

my name is Joseph and my father's name is Brian

该语句按单词拆分,如下表:
------------------------------
| ID | word |
------------------------------
| 1 | my |
| 2 | name |
| 3 | is |
| 4 | Joseph |
| 5 | and |
| 6 | my |
| 7 | father's |
| 8 | name |
| 9 | is |
| 10 | Brian |
------------------------------

我想获得每个单词的上一个和下一个单词

例如,我想获取“名称”的上一个词和下一个词:
--------------------------
| my | name | is |
--------------------------
| father's | name | is |
--------------------------

我怎么能得到这个结果?

最佳答案

您没有指定DBMS,因此以下是ANSI SQL:

select prev_word, word, next_word
from (
select id,
lag(word) over (order by id) as prev_word,
word,
lead(word) over (order by id) as next_word
from words
) as t
where word = 'name';

SQLFiddle: http://sqlfiddle.com/#!12/7639e/1

关于sql - 从使用(WHERE)条件选择的行中获取上一行和下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27086377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com