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

前端 未结 4 920
天命终不由人
天命终不由人 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:07

    I just needed to do something similar, but took a different approach. I copied the relevant string into a temp table where I can add columns to track an index through each occurrence on each row.

    In my example, I'm looking for substrings " - " (space-dash-space) in product descriptions, with the intent of eventually chopping those apart to show as bullet points, and I'm analyzing the data like this to see how many "bullets" products typically have.

    I suspect this is more efficient than repeatedly re-writing string values, but I haven't actually benchmarked.

    SELECT 
            ccp.ProductID, p.ProductDescription, descrlen=LEN(p.ProductDescription), 
            bulletcnt=0, indx=0, lastmatchat=0
        INTO #DescrBullets
        FROM Private.CompositeCatalogProduct AS ccp WITH(NOLOCK) 
        INNER JOIN Products.Product AS p WITH(NOLOCK) ON p.ProductId = ccp.ProductID
        WHERE ccp.CompositeCatalogID=53
    
    
    DECLARE @rows INT = 1
    WHILE @rows>0
    BEGIN 
    
    -- find the next occurrence on each row that's still in play
    UPDATE #DescrBullets
        SET lastmatchat = PATINDEX('% - %',RIGHT(ProductDescription,descrlen-indx))
        WHERE indx0
    SET @rows = @@ROWCOUNT
    
    -- for all the ones that didn't have a match, advance indx past the end
    -- so we don't need to reprocess on next iterations
    UPDATE #DescrBullets
        SET indx=descrlen
        WHERE lastmatchat=0
    
    RAISERROR('processing, %d products still have bullets', 0, 1, @rows) WITH NOWAIT
    
    END 
    
    SELECT db.bulletcnt, occurs=COUNT(*)
        FROM #DescrBullets AS db
        GROUP BY db.bulletcnt
        ORDER BY 1
    

提交回复
热议问题