【发布时间】:2020-11-14 05:02:04
【问题描述】:
假设您在列中有一个字符串,例如“The quick brown dog jumped over the lazy fox”。如何查询字符串中是否存在两个单词,但按特定顺序?
例如,如果我想按特定顺序检索包含 fox 和 dog 的所有记录。如果我们有以下字符串,我们应该只得到第二条记录,而不是第一条记录:
The quick brown dog jumped over the lazy fox (should not be retrieved)
The quick brown fox jumped over the lazy dog (retrieved due to order)
常规查询不会保持先搜索fox再搜索dog的顺序:
SELECT d.text
from docs d
where lower(d.text) ~ '\yfox\y' and
lower(d.text) ~ '\ydog\y'
如何保持查询词的优先级?我正在考虑使用lookahead 或lookbehind,但这个词可以是多个词彼此分开。
谢谢
【问题讨论】:
标签: sql postgresql regex-lookarounds