Database Articles
  Home arrow Database Articles arrow Page 4 - Searching with Strings
Moblin
Try It Free
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE ARTICLES

Searching with Strings
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-09-20

    Table of Contents:
  • Searching with Strings
  • 5.15 Using FULLTEXT Searches
  • Using FULLTEXT Searches continued
  • Narrowing the Search

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Try It Free
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Searching with Strings - Narrowing the Search


    (Page 4 of 4 )

    You can include additional criteria to narrow the search further. The following queries perform progressively more specific searches to find out how often the name Abraham occurs in the entire KJV, the New Testament, the Book of Hebrews, and Chapter 11 of Hebrews:

      mysql> SELECT COUNT(*) from kjv WHERE MATCH(vtext) AGAINST('Abraham');
      +----------+
      | COUNT(*) |
      +----------+
      |      216 |
      +----------+
      mysql> SELECT COUNT(*) from kjv
         -> WHERE MATCH(vtext) AGAINST('Abraham')
         -> AND bsect = 'N';
      +----------+
      | COUNT(*) |
      +----------+
      |       66 |
      +----------+
      mysql> SELECT COUNT(*) from kjv
         -> WHERE MATCH(vtext) AGAINST('Abraham')
        
    -> AND bname = 'Hebrews';
      +----------+
      | COUNT(*) |
      +----------+
      |       10 |
      +----------+
      mysql> SELECT COUNT(*) from kjv
         -> WHERE MATCH(vtext) AGAINST('Abraham')
        
    -> AND bname = 'Hebrews' AND cnum = 11;
     
    +----------+
      | COUNT(*) |
      +----------+
      |        2 |
      +----------+

    If you expect to use search criteria that include other non-FULLTEXT columns frequently, you can increase the performance of such queries by adding regular indexes to those columns. For example, to index the book, chapter, and verse number columns, do this:

      mysql> ALTER TABLE kjv ADD INDEX (bnum), ADD INDEX (cnum), ADD INDEX (vnum);

    Search strings in FULLTEXT queries can include more than just a single word, and you might suppose that adding additional words would make a search more specific. But in fact that widens it, because a FULLTEXT search returns rows that contain any of the words. In effect, the query performs an OR search for any of the words. This is illustrated by the following queries, which identify successively larger numbers of verses as additional search words are added:

      mysql> SELECT COUNT(*) from kjv
         
    -> WHERE MATCH(vtext) AGAINST('Abraham');
     
    +----------+
      | COUNT(*) |
      +----------+
      |      216 |
      +----------+
      mysql> SELECT COUNT(*) from kjv
         -> WHERE MATCH(vtext) AGAINST('Abraham Sarah');
      +----------+
      | COUNT(*) |
      +----------+
      |      230 |
      +----------+
      mysql> SELECT COUNT(*) from kjv
         -> WHERE MATCH(vtext) AGAINST('Abraham Sarah Ishmael Isaac');
      +----------+
      | COUNT(*) |
      +----------+
      |      317 |
      +----------+

    To perform a search in which each word in the search string must be present, see Recipe 5.17 .

    If you want to use a FULLTEXT search that looks through multiple columns simultaneously, name them all when you construct the index:

      ALTER TABLE tbl_name ADD FULLTEXT (col1, col2, col3);

    To issue a search query that uses this index, name those same columns in the MATCH() list:

      SELECT ... FROM tbl_name
     
    WHERE MATCH(col1, col2, col3) AGAINST('search string');

    You’ll need one such FULLTEXT index for each distinct combination of columns that you want to search.

    See Also

    FULLTEXT indexes provide a quick-and-easy way to set up a basic search engine. One way to use this capability is to provide a web-based interface to the indexed text. This book’s web site (see Appendix A) includes a simple web-based KJV search page that demonstrates this. You can use it as the basis for your own search engine that operates on a different repository of text.

    Please check back next week for the conclusion to this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    Buy this book now. This article is excerpted from chapter five of the MySQL Cookbook, Second Edition, written by Paul DuBois (O'Reilly; ISBN: 059652708X). Check it out today at your favorite bookstore. Buy this book now.

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway