mysql:10859
From: umemoto <umemoto <umemoto@xxxxxxxxxx>>
Date: Mon, 31 Jan 2005 18:52:54 +0900
Subject: [mysql 10859] Full-Textによる全文検索について
いつもお世話になっております。 うめもとです。 Full-Textを用いた全文検索についての質問を致します。 以下の1.のテーブルに対して、A.B.のような結果を得たいのですが、B.の結果を 得る手段が思い浮かびません。どなたか、ご存知でしたら教えて頂けませんでしょうか。 また、A.の方法以外で同じ結果を得られ、より処理速度の速いSQL文をご存知の方おりま したらお教え頂けませんでしょうか。 A.サーチワードを「test」とし、サーチワードを含む行のカウント数でのソートを行う。 B.サーチワードを「test」とし、ドキュメント内にtestをより多く含んでいるレコード 順でのソートを行う。 1.SELECT * FROM article; +----+------------------------------+------------------------------------------+ | id | title | body | +----+------------------------------+------------------------------------------+ | 1 | MySQL Tutorial | DBMS stands for DataBase ... | | 2 | How To Use MySQL Efficiently | After you went through a ... | | 3 | Optimizing MySQL | In this tutorial we will show ... | | 4 | 1001 MySQL Tricks | 1. Never run mysqld as root. 2. ... | | 5 | MySQL vs. YourSQL | In the following database comparison ... | | 6 | MySQL Security | When configured properly, MySQL ... | | 7 | test | test | | 8 | test | test test | | 9 | test | test test test | | 10 | test | test test test test | | 11 | test | test test test test test | | 12 | test | test test test test test test | | 13 | test | test test test test test test test | | 14 | test | test test test test test test test test | | 15 | aaaa | aaaa | | 16 | bbbb | bbbbb | | 17 | bbbb | ccccc | | 18 | bbbb | ddddd | | 19 | test | test test | | 20 | test | test test | +----+------------------------------+------------------------------------------+ 2.サーチワードを「test」とし、サーチワードを含む行のカウント数でのソートを行う。 SELECT id,body,count(body LIKE ('%test%')) AS ct FROM articles WHERE MATCH (title,body) AGAINST ('test' IN BOOLEAN MODE) AND body LIKE ('%test%') GROUP BY body ORDER BY ct DESC; +----+-----------------------------------------+----+ | id | body | ct | +----+-----------------------------------------+----+ | 8 | test test | 3 | | 7 | test | 1 | | 9 | test test test | 1 | | 10 | test test test test | 1 | | 11 | test test test test test | 1 | | 12 | test test test test test test | 1 | | 13 | test test test test test test test | 1 | | 14 | test test test test test test test test | 1 | +----+-----------------------------------------+----+ 3.サーチワードを「test」とし、ドキュメント内にtestをより多く含んでいるレコード 順でのソートを行う。 +----+------------------------------+------------------------------------------+ | id | title | body | +----+------------------------------+------------------------------------------+ | 14 | test | test test test test test test test test | | 13 | test | test test test test test test test | | 12 | test | test test test test test test | | 11 | test | test test test test test | | 10 | test | test test test test | | 9 | test | test test test | | 8 | test | test test | | 19 | test | test test | | 20 | test | test test | | 7 | test | test | +----+------------------------------+------------------------------------------+ 【テーブルデータ】 DROP TABLE IF EXISTS `articles`; CREATE TABLE `articles` ( `id` int(10) unsigned NOT NULL auto_increment, `title` varchar(200) default NULL, `body` text, PRIMARY KEY (`id`), FULLTEXT KEY `title` (`title`,`body`) ) ENGINE=MyISAM DEFAULT CHARSET=sjis; LOCK TABLES `articles` WRITE; INSERT INTO `articles` VALUES (1,'MySQL Tutorial','DBMS stands for DataBase ...' ),(2,'How To Use MySQL Efficiently','After you went through a ...'),(3,'Optimizi ng MySQL','In this tutorial we will show ...'),(4,'1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),(5,'MySQL vs. YourSQL','In the following database c omparison ...'),(6,'MySQL Security','When configured properly, MySQL ...'),(7,'t est','test'),(8,'test','test test'),(9,'test','test test test'),(10,'test','test test test test'),(11,'test','test test test test test'),(12,'test','test test t est test test test'),(13,'test','test test test test test test test'),(14,'test' ,'test test test test test test test test'),(15,'aaaa','aaaa'),(16,'bbbb','bbbbb '),(17,'bbbb','ccccc'),(18,'bbbb','ddddd'),(19,'test','test test'),(20,'test','t est test'); UNLOCK TABLES;