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 word', `my_column`) AS `match_count`
FROM `table`
Thanks!
EDIT:
I need to know how many times the string appears in a column for each row in a SELECT
.
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.
Your scenario:
If you take out the "where" function, it will count the number of times each distinct entry appears in "my_column".
Depends what you mean by "string occurs" - as a substring or as full value?
Full value case:
Substring case:
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.
An obvious but unscalable way is like this
Have you investigated Full Text search in MySQL?