代码之家  ›  专栏  ›  技术社区  ›  MTK

选择以开头但不存在于其他表中的单词

  •  0
  • MTK  · 技术社区  · 6 年前

    我需要从 phrase_table phrase 列包含以开头的单词 be (在本例中)但如果整个单词存在于 stop_words_table 如果一个单词出现在 但另一个不存在(见id=4 because

    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 回复  |  直到 6 年前
        1
  •  1
  •   Gordon Linoff    6 年前

    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);