MySQL query finding values in a comma separated string

后端 未结 11 1402
孤独总比滥情好
孤独总比滥情好 2020-11-21 23:40

I have a field COLORS (varchar(50)) in a my table SHIRTS that contains a comma delimited string such as 1,2,5,12,15,. Each number repr

11条回答
  •  遥遥无期
    2020-11-21 23:53

    You can achieve this by following function.

    Run following query to create function.

    DELIMITER ||
    CREATE FUNCTION `TOTAL_OCCURANCE`(`commastring` TEXT, `findme`     VARCHAR(255)) RETURNS int(11)
    NO SQL
    -- SANI: First param is for comma separated string and 2nd for string to find.
    return ROUND (   
        (
            LENGTH(commastring)
            - LENGTH( REPLACE ( commastring, findme, "") ) 
        ) / LENGTH(findme)        
    );
    

    And call this function like this

    msyql> select TOTAL_OCCURANCE('A,B,C,A,D,X,B,AB', 'A');
    

提交回复
热议问题