Well when i start to think that something is hard then i try to get it done...
Here is a solution : (TheName of the Database is 'Test')
1st create this function
DELIMITER $$
CREATE DEFINER = 'root'@'%'
FUNCTION Test.countOccurence (LineTocheck nvarchar(255), criteriaToMatch nvarchar(15))
RETURNS int(11)
BEGIN
DECLARE Occurences int DEFAULT 0;
SELECT
(LENGTH(LineTocheck) - LENGTH(REPLACE(LineTocheck, criteriaToMatch, ''))) / LENGTH(criteriaToMatch) INTO Occurences;
RETURN Occurences;
END
$$
DELIMITER ;
2nd you execute the query :
SELECT Generic.id
,Description
,SUM(countOccurence(Description, c.criteria))
FROM Generic
,criteria c
GROUP BY Description
,Generic.id
ORDER BY SUM(countOccurence(Description, c.criteria)) desc
P.S. the table structure is :
For the Criteria :
CREATE TABLE Test.criteria (
id int(11) NOT NULL AUTO_INCREMENT,
criteria varchar(15) NOT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci;
For the table you want to search the Occurences
CREATE TABLE Test.Generic (
id int(11) NOT NULL AUTO_INCREMENT,
Description varchar(255) NOT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_general_ci;
SET NAMES 'utf8';
INSERT INTO Test.criteria(id, criteria) VALUES
(1, 'fox');
INSERT INTO Test.criteria(id, criteria) VALUES
(2, 'brown');
INSERT INTO Test.criteria(id, criteria) VALUES
(3, 'over');
SET NAMES 'utf8';
INSERT INTO Test.Generic(id, Description) VALUES
(1, 'the quick brown fox');
INSERT INTO Test.Generic(id, Description) VALUES
(2, 'the quick brown fox jumps');
INSERT INTO Test.Generic(id, Description) VALUES
(3, 'the quick brown fox jumps over the lazy dog');
INSERT INTO Test.Generic(id, Description) VALUES
(4, 'the quick potato does nothing');
Use Dbforge MySQL Studio Express (free) to connect to MySQL and run the statements
http://www.devart.com/login.html?returnToUrl=/dbforge/mysql/studio/download.html%3Ffd=dbforgemysqlfree.exe
Test it and let me know