选择是否以其他单词开头但不存在的单词

时间:2019-01-28 01:27:22

标签: mysql sql

我需要从phrase_table中获取所有行,其中phrase列包含以be开头的单词(在此示例中),但是如果整个单词都存在于stop_words_table中,则要排除如果一个词存在于stop_words_table中,但另一个词不存在,则不排除(请参见id = 4 bebecause

phrase_table
id  phrase
1     would be fine
2     nothing to do
3     belgium is beautiful
4     this also must be included because I need

stopwords_table
id    word
1       be

我已经尝试过了:

SELECT id FROM phrase_table
WHERE phrase REGEXP '[[:<:]]be' = 1
-- That return id 1,3,4

SELECT id FROM phrase_table
WHERE phrase REGEXP '[[:<:]]be' = 1
AND phrase NOT IN(
    SELECT * FROM stopwords_table WHERE word = 'be'
)
-- That return nothing because 'be' exists in stopwords_table

预期结果

id 3 and 4 from phrase_table

1 个答案:

答案 0 :(得分:1)

嗯。如果空格是单词之间的分隔符,那么我在想:

select *
from phrase_table pt
where concat(' ', pt.phrase, ' ') regexp '[^ ]be|be[^ ]';

您可以使用类似以下内容将其扩展为join

select sw.word, pt.*
from phrase_table pt join
     stopwords_table sw
     on concat(' ', pt.phrase, ' ') regexp replace('[^ ]@sw|@sw[^ ]', '@sw', sw.word);