php - how to count the words in a sentence in mysql pdo -


how go counting words in sentence in database, example this:

id words  1  bananas, mango, orange, watermelon  2  bananas, mango, orange, watermelon  3  bananas, mango, orange, watermelon  4  bananas, mango, orange, watermelon 

i want count number of mango, if count , result : 4

this query

"select count(words)as total fuit ?????"; 

?which means not know how use where..

but how query in mysql , pdo?

if there no spaces in words field (e.g. bananas,mango,orange,watermelon), use find_in_set:

 select ... find_in_set('mango', words) 

with spaces, have use regexes, example:

select ... words rlike '(^|, )mango($|,)' 

better yet, consider "normalizing" data - make separate words table:

id  word 1   mango 2   orange etc 

and linkage table:

doc_id    word_id 1         1 1         2 etc 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -