sql >> Base de Datos >  >> RDS >> Mysql

Cómo encontrar palabras repetidas de una celda en SQL

Si desea codificarlo de forma rígida:

select EntityID, Situation
from Entity
where Situation like '%the the%'
or Situation like '%of of%'
or Situation like '%is is%'

Actualización: Aquí hay un enfoque un poco menos codificado:

select EntityID, Situation, right(s2, diff * 2 + 1) as RepeatedWords
from (
    select EntityID, Situation, WordNumber,
        substring_index(Situation, ' ', WordNumber) s1,
        substring_index(Situation, ' ', WordNumber + 1) s2,
        length(substring_index(Situation, ' ', WordNumber + 1)) - length(substring_index(Situation, ' ', WordNumber)) -1 diff
    from `Entity` e
    inner join (
        select 1 as WordNumber
        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 
        union all
        select 10 
    ) n
) a
where right(s1, diff) = right(s2, diff)
    and diff > 0
order by EntityID, WordNumber

Buscará hasta las primeras 10 palabras más o menos, y no maneja adecuadamente las mayúsculas y minúsculas, la puntuación o los espacios múltiples, pero debería darle una idea de un enfoque que puede tomar. Si desea que maneje cadenas más largas, siga agregando a las instrucciones UNION ALL.