Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Extrahieren Sie eine Suchzeichenfolge im Kontext

Hier ist das SQL, das Sie brauchen:

SELECT
    id,
    title,
    substring(body,  
        case
             when locate('mysql', lower(body)) <= 20 then 1
             else locate('mysql', lower(body)) - 20
        end,
        case
            when locate('mysql', lower(body)) + 20 > length(body) then length(body)
            else locate('mysql', lower(body)) + 20
        end)
FROM content
WHERE lower(body) LIKE '%mysql%'
    OR title LIKE '%mysql%'
limit 8;

FYI:Getestet und funktioniert.