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

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

I have a table like this:

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


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

    In SQL SERVER, this is the answer

    Declare @t table(TITLE VARCHAR(100), DESCRIPTION VARCHAR(100))
    
    INSERT INTO @t SELECT 'test1', 'value blah blah value' 
    INSERT INTO @t SELECT 'test2','value test' 
    INSERT INTO @t SELECT 'test3','test test test' 
    INSERT INTO @t SELECT 'test4','valuevaluevaluevaluevalue' 
    
    
    SELECT TITLE,DESCRIPTION,Count = (LEN(DESCRIPTION) - LEN(REPLACE(DESCRIPTION, 'value', '')))/LEN('value') 
    
    FROM @t
    

    Result

    TITLE   DESCRIPTION               Count
    test1   value blah blah value        2
    test2   value test                   1
    test3   test test test               0
    test4   valuevaluevaluevaluevalue    5
    

    I don't have MySQL install, but goggled to find the Equivalent of LEN is LENGTH while REPLACE is same.

    So the equivalent query in MySql should be

    SELECT TITLE,DESCRIPTION, (LENGTH(DESCRIPTION) - LENGTH(REPLACE(DESCRIPTION, 'value', '')))/LENGTH('value') AS Count
    FROM <yourTable>
    

    Please let me know if it worked for you in MySql also.

    0 讨论(0)
  • 2020-11-22 08:36

    A little bit simpler and more effective variation of @yannis solution:

    SELECT 
        title,
        description,    
        CHAR_LENGTH(description) - CHAR_LENGTH( REPLACE ( description, 'value', '1234') ) 
            AS `count`    
    FROM <table> 
    

    The difference is that I replace the "value" string with a 1-char shorter string ("1234" in this case). This way you don't need to divide and round to get an integer value.

    Generalized version (works for every needle string):

    SET @needle = 'value';
    SELECT 
        description,    
        CHAR_LENGTH(description) - CHAR_LENGTH(REPLACE(description, @needle, SPACE(LENGTH(@needle)-1))) 
            AS `count`    
    FROM <table> 
    
    0 讨论(0)
  • 2020-11-22 08:38

    This should do the trick:

    SELECT 
        title,
        description,    
        ROUND (   
            (
                LENGTH(description)
                - LENGTH( REPLACE ( description, "value", "") ) 
            ) / LENGTH("value")        
        ) AS count    
    FROM <table> 
    
    0 讨论(0)
  • 2020-11-22 08:38

    try this:

     select TITLE,
            (length(DESCRIPTION )-length(replace(DESCRIPTION ,'value','')))/5 as COUNT 
      FROM <table> 
    


    SQL Fiddle Demo

    0 讨论(0)
  • 2020-11-22 08:39

    This is the mysql function using the space technique (tested with mysql 5.0 + 5.5): CREATE FUNCTION count_str( haystack TEXT, needle VARCHAR(32)) RETURNS INTEGER DETERMINISTIC RETURN LENGTH(haystack) - LENGTH( REPLACE ( haystack, needle, space(char_length(needle)-1)) );

    0 讨论(0)
  • 2020-11-22 08:45
    SELECT 
    id,
    jsondata,    
    ROUND (   
        (
            LENGTH(jsondata)
            - LENGTH( REPLACE ( jsondata, "sonal", "") ) 
        ) / LENGTH("sonal")        
    )
    +
    ROUND (   
        (
            LENGTH(jsondata)
            - LENGTH( REPLACE ( jsondata, "khunt", "") ) 
        ) / LENGTH("khunt")        
    )
    AS count1    FROM test ORDER BY count1 DESC LIMIT 0, 2
    

    Thanks Yannis, your solution worked for me and here I'm sharing same solution for multiple keywords with order and limit.

    0 讨论(0)
提交回复
热议问题