SQL function to get count of how many times string appears in column?

前端 未结 4 935
天命终不由人
天命终不由人 2020-12-17 06:29

Is there a function for MySQL that will count the number of times a string occurs in another string or column? Basically I want:

SELECT
    SUB_COUNT(\'my wo         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 07:18

    I think you may be able to use the following example. I was trying to count the number of times a particular carton type was used when shipping.

    SELECT carton_type, COUNT(carton_type) AS match_count
    FROM carton_hdr
    WHERE whse ='wh1'
    GROUP BY "carton_type"
    

    Your scenario:

    SELECT my_column COUNT(my_column)
    FROM my_table
    WHERE my_column = 'my_word'
    GROUP BY my_column
    

    If you take out the "where" function, it will count the number of times each distinct entry appears in "my_column".

提交回复
热议问题