0

How can I filter my model MyModel on the basis of the number of occurences of a certain phrase in a text field in MyModel

I guess it can't be done with django orm, perhaps an SQL query can help?

Something like:

MyModal.objects.extra(count="SQL query that finds occurence count of a certain phrase in certain model field")

A complete raw query is fine too.

will-hart
  • 3,742
  • 2
  • 38
  • 48
ashish
  • 2,090
  • 3
  • 17
  • 16
  • I guess what you really need is the query, and you can find that in the answer to [this question](http://stackoverflow.com/questions/748276/using-sql-to-determine-word-count-stats-of-a-text-field) – Burhan Khalid Sep 07 '12 at 10:37
  • thanks @BurhanKhalid, but the link you gave has suggested to process data outside db which is something i don't want to do. – ashish Sep 07 '12 at 12:44
  • 1
    You write the function in MySQL directly. Its not external. See my answer to [this question](http://stackoverflow.com/questions/12156970/mysql-count-word-in-sql-syntax/12157152#12157152) for an example on how to use it. – Burhan Khalid Sep 08 '12 at 06:46

1 Answers1

0

try this:

set @str='a b a c';
select length(@str)-length(replace(@str,'a','')) as 'number of occuarence'
Joe G Joseph
  • 23,518
  • 5
  • 56
  • 58