Count the number of occurrences of a string in a VARCHAR field?

后端 未结 7 1158
花落未央
花落未央 2020-11-22 07:51

I have a table like this:

TITLE          |   DESCRIPTION
------------------------------------------------
test1          |   value blah blah value
test2              


        
相关标签:
7条回答
  • 2020-11-22 08:47

    Here is a function that will do that.

    CREATE FUNCTION count_str(haystack TEXT, needle VARCHAR(32))
      RETURNS INTEGER DETERMINISTIC
      BEGIN
        RETURN ROUND((CHAR_LENGTH(haystack) - CHAR_LENGTH(REPLACE(haystack, needle, ""))) / CHAR_LENGTH(needle));
      END;
    
    0 讨论(0)
提交回复
热议问题