Unfortunately, it's not as easy as you think it will be. Phrases will have to be split into individual word rows by cross joining a makeshift index table and sub-splitting the phrase into parts.
You'll have to do something like the following, which will pull the first 20 words in your search phrase. You can just add unions on the ints table to get more words.
Code:
SELECT word, COUNT(1) AS word_count FROM
(SELECT *,
SUBSTRING_INDEX(SUBSTRING_INDEX(s.phrase,' ',ints.i),' ',-1) AS word
FROM (SELECT 1 AS i UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10
UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20) AS ints
CROSS JOIN search AS s
WHERE ints.i BETWEEN 1 and (SELECT 1 + LENGTH(s.phrase) - LENGTH(REPLACE(s.phrase,' ','')))) AS words
GROUP BY word
ORDER BY word_count DESC;
If you want to eliminate common words, you'll need to do this:
Code:
CREATE TABLE common_words (word VARCHAR(255) NOT NULL PRIMARY KEY);
INSERT INTO common_words (word) VALUES ('the'), ('a'), ('in'), ('of'), ('to'), ('and'), ('for'), ('is'), ('we'), ('are'), ('that'), ('have'), ('been'), ('etc');
-- Then your query will be altered as follows:
SELECT words.word, COUNT(1) AS word_count, COUNT(c.word) AS common FROM
(SELECT *,
SUBSTRING_INDEX(SUBSTRING_INDEX(s.phrase,' ',ints.i),' ',-1) AS word
FROM (SELECT 1 AS i UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10
UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20) AS ints
CROSS JOIN search AS s
WHERE ints.i BETWEEN 1 and (SELECT 1 + LENGTH(s.phrase) - LENGTH(REPLACE(s.phrase,' ','')))) AS words
LEFT JOIN common_words AS c ON words.word = c.word
GROUP BY words.word
HAVING common = 0
ORDER BY word_count DESC
Note that these may be performance hogs if you have lots of searches... you may eventually want to create a second table with just search words, which will perform exponentially faster in word queries.