Count occurrences of a word in a row in MySQL

后端 未结 9 1018
野趣味
野趣味 2020-12-19 08:41

I\'m making a search function for my website, which finds relevant results from a database. I\'m looking for a way to count occurrences of a word, but I need to ensure that

9条回答
  •  忘掉有多难
    2020-12-19 09:18

    You can try this perverted way:

    SELECT 
    (LENGTH(field) - LENGTH(REPLACE(field, 'word', ''))) / LENGTH('word') AS `count`
    ORDER BY `count` DESC
    
    • This query can be very slow
    • It looks pretty ugly
    • REPLACE() is case-sensitive

提交回复
热议问题