ºìÁªLinuxÃÅ»§
Linux°ïÖú

ÔÚMySQLÖлñµÃ¸üºÃµÄËÑË÷½á¹û

·¢²¼Ê±¼ä:2006-04-09 00:56:19À´Ô´:ºìÁª×÷Õß:Çç
ºÜ¶à»¥ÁªÍøÓ¦ÓóÌÐò¶¼ÌṩÁËÈ«ÎÄËÑË÷¹¦ÄÜ£¬Óû§¿ÉÒÔʹÓÃÒ»¸ö´Ê»òÕß´ÊÓïƬ¶Ï×÷Ϊ²éѯÏîÄ¿À´¶¨Î»Æ¥ÅäµÄ¼Ç¼¡£ÔÚºǫ́£¬ÕâЩ³ÌÐòʹÓÃÔÚÒ»¸öSELECT²éѯÖеÄLIKEÓï¾äÀ´Ö´ÐÐÕâÖÖ²éѯ£¬¾¡¹ÜÕâÖÖ·½·¨¿ÉÐУ¬µ«¶ÔÓÚÈ«ÎIJéÕÒ¶øÑÔ£¬ÕâÊÇÒ»ÖÖЧÂʼ«¶ËµÍϵķ½·¨£¬ÓÈÆäÔÚ´¦Àí´óÁ¿Êý¾ÝµÄʱºò¡£

¡¡¡¡MySQLÕë¶ÔÕâÒ»ÎÊÌâÌṩÁËÒ»ÖÖ»ùÓÚÄÚ½¨µÄÈ«ÎIJéÕÒ·½Ê½µÄ½â¾ö·½°¸¡£ÔÚ´Ë£¬¿ª·¢ÕßÖ»ÐèÒª¼òµ¥µØ±ê¼Ç³öÐèҪȫÎIJéÕÒµÄ×ֶΣ¬È»ºóʹÓÃÌØÊâµÄMySQL·½·¨ÔÚÄÇЩ×Ö¶ÎÔËÐÐËÑË÷£¬Õâ²»½ö½öÌá¸ßÁËÐÔÄܺÍЧÂÊ£¨ÒòΪMySQL¶ÔÕâЩ×Ö¶Î×öÁËË÷ÒýÀ´ÓÅ»¯ËÑË÷£©£¬¶øÇÒʵÏÖÁ˸ü¸ßÖÊÁ¿µÄËÑË÷£¬ÒòΪMySQLʹÓÃ×ÔÈ»¡¡¡¡ÓïÑÔÀ´ÖÇÄܵضԽá¹ûÆÀ¼¶£¬ÒÔÈ¥µô²»Ïà¹ØµÄÏîÄ¿¡£

¡¡¡¡ÕâƪÎÄÕ½«ÏòÄú½²ÊöÔÚMySQLÖÐÈçºÎ½øÐÐÈ«ÎÄËÑË÷¡£

¡¡¡¡1¡¢ÉèÖûù±¾±í¸ñ

¡¡¡¡´Ó´´½¨Àý×Ó±í¸ñ¿ªÊ¼£¬Ê¹ÓÃÒÔϵÄSQLÃüÁ


¡¡¡¡mysql> CREATE TABLE reviews (id INT(5) PRIMARY KEY NOT NULL AUTO_INCREMENT, data TEXT);



¡¡¡¡ÒÔÉÏÃüÁî´´½¨ÁËÒ»¸ö¼òµ¥µÄÒôÀÖר¼¯×ÊÁϿ⣨Ö÷ÒªÊÇÕû¶ÎµÄÎÄ×Ö£©£¬È»ºóÏòÕâ¸ö±í¸ñÖÐÌí¼ÓһЩ¼Ç¼£º


mysql> INSERT INTO `reviews` (`id`, `data`) VALUES(1, 'Gingerboy has a new single out called Throwing Rocks. It\'s great!');mysql> INSERT INTO `reviews` (`id`, `data`) VALUES (2, 'Hello all, I really like the new Madonna single. One of the hottest tracks currently playing...I\'ve been listening to it all day');mysql> INSERT INTO `reviews` (`id`, `data`)VALUES (3, 'Have you heard the new band Hotter Than Hell?They have five members and they burn their instruments when they play in concerts. These guys totally rock! Like, awesome, dude!');



¡¡¡¡ÑéÖ¤Êý¾ÝµÄÕýȷ¼È룺


mysql> SELECT * FROM reviews;+----+--------------------------------------------+| id | data |+----+--------------------------------------------+| 1 | Gingerboy has a new single out called ... || 2 | Hello all, I really like the new Madon ... || 3 | Have you heard the new band Hotter Than... |+----+--------------------------------------------+3 rows in set (0.00 sec)


2¡¢¶¨ÒåÈ«ÎÄËÑË÷×Ö¶Î

¡¡¡¡½ÓÏÂÀ´£¬¶¨ÒåÄúÒª×÷ΪȫÎÄËÑË÷Ë÷ÒýµÄ×Ö¶Î


mysql> ALTER TABLE reviews ADD FULLTEXT INDEX (data);Query OK, 3 rows affected (0.21 sec)Records: 3 Duplicates: 0 Warnings: 0



¡¡¡¡Ê¹ÓÃSHOW INDEXESÃüÁîÀ´¼ì²éË÷ÒýÒѾ­±»Ìí¼ÓÁË£º


mysql> SHOW INDEXES FROM reviews;+---------+---------------+--------+------+------------+---------+| Table | Column_name | Packed | Null | Index_type | Comment |----------+---------------+--------+------+------------+---------+| reviews | id | NULL | | BTREE | || reviews | data | NULL | YES | FULLTEXT | |+---------+---------------+--------+------+------------+---------+




¡¡¡¡3¡¢ÔËÐÐÈ«ÎÄËÑË÷

¡¡¡¡µ±ÄúÓµÓÐÁËÊý¾ÝºÍË÷Òý£¬¾Í¿ÉÒÔʹÓÃMySQLµÄÈ«ÎÄËÑË÷ÁË£¬×î¼òµ¥µÄÈ«ÎÄËÑË÷·½Ê½ÊÇ´øÓÐMATCH...AGAINSTÓï¾äµÄSELECT²éѯ£¬ÒÔÏÂÊÇÒ»¸ö¼òµ¥µÄÀý×Ó£¬¿ÉÒÔÀ´²éÕÒº¬Óе¥´Ê¡°single¡±µÄ¼Ç¼£º


mysql> SELECT id FROM reviews WHERE MATCH (data) AGAINST ('single');+----+| id |+----+| 1 || 2 |+----+


2 rows in set (0.00 sec)

¡¡¡¡ÔÚ´Ë£¬MATCH()½«×÷Ϊ²ÎÊý´«µÝ¸øËüµÄ×Ö¶ÎÖеÄÎÄ×ÖÓë´«µÝ¸øAGAINST()µÄ²ÎÊý½øÐбȽϣ¬Èç¹ûÓÐÆ¥ÅäµÄ£¬ÄǾͰ´ÕÕÕý³£µÄ·½Ê½·µ»Ø¡£×¢ÒâÄú¿ÉÒÔ´«µÝ²»Ö¹Ò»¸ö×Ö¶ÎÓÃMATCH()À´²é¿´­£­Ö»ÐèÓöººÅÀ´·Ö¸î×Ö¶ÎÁÐ±í¡£

¡¡¡¡µ±MySQLÊÕµ½ÁËÒ»¸öÈ«ÎÄËÑË÷µÄÇëÇó£¬Ëü¾ÍÔÚÄÚ²¿¶Ôÿ¸ö¼Ç¼½øÐÐÆÀ·Ö£¬²»Æ¥ÅäµÄ¼Ç¼µÃ·ÖΪÁ㣬¶ø¡°¸üÏà¹Ø¡±µÄ¼Ç¼»áµÃµ½±È¡°²»Ì«Ïà¹Ø¡±µÄ¼Ç¼Ïà¶Ô¸ü¸ßµÄ·ÖÊý¡£Ïà¹ØÐÔÊÇÓÉMySQLµÄһϵÁÐÇø·Ö±ê×¼À´¾ö¶¨µÄ£¬²é¿´MySQLµÄÓû§ÊÖ²á¿ÉÒԵõ½¸ü¶àµÄÐÅÏ¢¡£

¡¡¡¡Ïë¿´µ½Ã¿¸ö¼Ç¼µÄÆÀ·ÖÈçºÎ£¬Ö»ÐèÒª·µ»ØMATCH()·½·¨×÷Ϊ½á¹û¼¯µÄÒ»²¿·Ö£¬ÈçÏÂËùʾ£º


mysql> SELECT id, MATCH (data) AGAINST ('rock') FROM reviews;+----+-------------------------------+| id | MATCH (data) AGAINST ('rock') |+----+-------------------------------+| 1 | 0 || 2 | 0 || 3 | 1.3862514533815 |+----+-------------------------------+




¡¡¡¡3 rows in set (0.00 sec)

¡¡¡¡4¡¢Ê¹ÓÃÂß¼­ËÑË÷ÐÞÊηû£¨Boolean search modifiers£©

¡¡¡¡Äú»¹¿ÉÒÔʹÓÃÂß¼­ËÑË÷ÐÞÊηûÀ´½øÐиü¾«È·µÄËÑË÷£¬Õâͨ¹ýÔÚAGAINSTÓï¾äÖÐÌí¼ÓÌØÊâµÄIN BOOLEAN MODEÐÞÊηûÀ´ÊµÏÖ£¬ÔÚÒÔϵÄÀý×ÓÖУ¬½«²éÕÒº¬Óе¥´Ê¡°single¡±µ«ÊÇûÓС°Madonna¡±µÄ¼Ç¼£º


mysql> SELECT id FROM reviews WHERE MATCH (data) AGAINST ('+single -madonna' IN BOOLEAN MODE);+----+| id |+----+| 1 |+----+


1 row in set (0.00 sec)

¡¡¡¡ÕâÒ»ËÑË÷ÌØÐÔͨ³£ÓÃÓÚËÑË÷µ¥´ÊƬ¶Ï£¨¶ø²»ÊÇÍêÕûµÄ´ÊÓ£¬Õâ¿ÉÒÔͨ¹ýÔÚIN BOOLEAN MODEÓï¾äÖеÄ*£¨ÐǺţ©²Ù×÷·ûÀ´ÊµÏÖ£¬ÒÔϵÄÀý×ÓչʾÁËÈçºÎ²éÕÒµ¥´ÊÖк¬ÓС°hot¡±µÄ¼Ç¼£º


mysql> SELECT id FROM reviews WHERE MATCH (data) AGAINST ('hot*' IN BOOLEAN MODE);+----+| id |+----+| 3 || 2 |+----+



¡¡¡¡2 rows in set (0.00 sec)

¡¡¡¡Äú»¹¿ÉÒÔʹÓÃÕâÖÖ·½·¨À´²éÕÒÖÁÉÙÒ»¸ö´«µÝµ½AGAINSTµÄ²ÎÊýÖУ¬ÒÔϵÄÀý×Ó²éÕÒÁËÖÁÉÙ°üº¬µ¥´Ê¡°hell¡±ºÍ¡°rocks¡±ÖеÄÒ»¸öµÄ¼Ç¼£º


mysql> SELECT id FROM reviews WHERE MATCH (data) AGAINST ('hell rocks' IN BOOLEAN MODE);+----+| id |+----+| 1 || 3 |+----+



¡¡¡¡3 rows in set (0.00 sec)

¡¡¡¡ÒÔÉϵÄÕâЩÀý×ÓÑÝʾÁËÏà¶ÔÓÚ´«Í³µÄSELECT...LIKEÓï¾ä£¬½øÐÐÈ«ÎÄËÑË÷µÄ¸üÓÐЧµÄ·½·¨£¬µ±ÄúÏÂÒ»´ÎÐèÒª±àдMySQLÊý¾Ý¿âËÑË÷½çÃæµÄʱºò£¬Äú¿ÉÒÔ³¢ÊÔÕâÒ»·½·¨¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ