How to find most popular word occurrences in MySQL?

后端 未结 6 2305
猫巷女王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 11:56

    This would give you single words (Just if I understand what your single word means.):

    select concat(val,' ',cnt) as result from(
        select (substring_index(substring_index(t.title, ' ', n.n), ' ', -1)) val,count(*) as cnt
            from result t cross join(
             select a.n + b.n * 10 + 1 n
             from 
                    (select 0 as n union all select 1 union all select 2 union all select 3 
                            union all select 4 union all select 5 union all select 6 
                            union all select 7 union all select 8 union all select 9) a,
                    (select 0 as n union all select 1 union all select 2 union all select 3 
                            union all select 4 union all select 5 union all select 6 
                            union all select 7 union all select 8 union all select 9) b
                    order by n 
            ) n
        where n.n <= 1 + (length(t.title) - length(replace(t.title, ' ', '')))
        group by val
        order by cnt desc
    ) as x
    

    Result should be looks like this :

    Result
    --------
    for 6
    sale 6
    house 2
    and 2
    cheap 2
    phones 1
    iphones 1
    dogs 1
    furniture 1
    cars 1
    androids 1
    cats 1
    

    But if the single word you need like this :

    result
    -----------
    for 6 sale 6 house 2 and 2 cheap 2 phones 1 iphones 1 dogs 1 furniture 1 cars 1 androids 1 cats 1
    

    Just modify the query above to:

    select group_concat(concat(val,' ',cnt) separator ' ') as result from( ...
    

提交回复
热议问题