Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 26th, 2012  |  syntax: None  |  size: 1.10 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Match partial words using MySQL Fulltext search?
  2. CREATE TABLE IF NOT EXISTS `items` (
  3.   `id` int(11) unsigned NOT NULL,
  4.   `irn` varchar(30) NOT NULL,
  5.   `name` varchar(225) NOT NULL,
  6.   `description` text,
  7.   PRIMARY KEY (`id`),
  8.   FULLTEXT KEY `name_desc_irn` (`name`,`description`,`irn`),
  9.   FULLTEXT KEY `name` (`name`),
  10.   FULLTEXT KEY `description` (`description`),
  11.   FULLTEXT KEY `irn` (`irn`)
  12. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
  13.        
  14. id    name        irn          description    
  15. 1     Widget A    ABC12345     testing ABC12345
  16. 2     Widget B    ABC-12345    the ABC is great
  17.        
  18. SELECT
  19.     items.id as id,
  20.     items.irn as irn,
  21.     items.name as name,
  22.     (
  23.         ((MATCH (irn) AGAINST ('12345')) * 5) +
  24.         ((MATCH (name) AGAINST ('12345')) * 4) +
  25.         ((MATCH (description) AGAINST ('12345')) * 3) +
  26.         (MATCH(name, description, irn) AGAINST ('12345'))
  27.     ) AS relevance
  28. FROM
  29.     items
  30. WHERE
  31.     MATCH(name, description, irn) AGAINST ('*12345' IN BOOLEAN MODE)
  32. HAVING
  33.     relevance > 0
  34. ORDER BY
  35.     relevance DESC
  36.        
  37. WHERE irn LIKE '%12345' OR name LIKE '%12345' OR description LIKE '%12345'