Mysql – Order result by number of occurence of a word in a text

MySQLorder-byselectsqlite

I am working with sqlite3/python .

I would like to order results by the number of occurence of %@% in text.

For exemple

Jimmy 10 // Jimmy wrote 10 strings like %@%
James 2
Sophia 0
..etc

The code could be something like this :

cursor = db.execute("SELECT name, text 
                       FROM MyTable
                      WHERE text LIKE '%@%'
                      ORDER BY someMagic DESC")

I am looking for someMagic if it exists 🙂

Best Answer

SELECT LENGTH(text)-LENGTH(REPLACE(text,'@',''))as counting from table
ORDER BY counting DESC

SQL Fiddle