How to find most popular word occurrences in MySQL?

后端 未结 6 2289
猫巷女王i
猫巷女王i 2021-02-13 11:38

I have a table called results with 5 columns.

I\'d like to use the title column to find rows that are say: WHERE title like \'%for sale%\

6条回答
  •  遥遥无期
    2021-02-13 12:06

    You can extract words with some string manipulation. Assuming you have a numbers table and that words are separated by single spaces:

    select substring_index(substring_index(r.title, ' ', n.n), ' ', -1) as word,
           count(*)
    from results r join
         numbers n
         on n.n <= length(title) - length(replace(title, ' ', '')) + 1
    group by word;
    

    If you don't have a numbers table, you can construct one manually using a subquery:

    from results r join
         (select 1 as n union all select 2 union all select 3 union all . . .
         ) n
         . . .
    

    The SQL Fiddle (courtesy of @GrzegorzAdamKowalski) is here.

提交回复
热议问题