Sunday, April 17, 2011

Full Text Search MySQL

Create a table called extypes, with 3 columns, ID , description and calpermin

CREATE TABLE IF NOT EXISTS `extypes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`description` text NOT NULL,
`calpermin` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

Insert some values into the table

INSERT INTO `extypes` (`id`, `description`, `calpermin`) VALUES
(1, 'Rowing Machine Moderate', 6),
(2, 'Walking Moderate', 4),
(3, 'Shopping With Trolley', 2),
(4, 'Dancing Moderate', 2),
(5, 'Dancing Vigorous', 4)

Make the description column searchable

ALTER TABLE `extypes` ADD FULLTEXT `search_index` (
`description`)

Search for the keyword "rowing" in the description

SELECT * from extypes WHERE MATCH (description) AGAINST('rowing')

No comments: