Counting Substring Instances and Ordering by Occurrence in MySQL
In MySQL, you can count the occurrences of a specific substring in a string field and subsequently sort the results based on the number of occurrences in a descending order.
Query:
The following query accomplishes this task:
SELECT (CHAR_LENGTH(str) - CHAR_LENGTH(REPLACE(str, substr, ''))) / CHAR_LENGTH(substr) AS cnt ... ORDER BY cnt DESC
Explanation:
The above is the detailed content of How to Count and Order Substring Occurrences in MySQL?. For more information, please follow other related articles on the PHP Chinese website!