Database Articles
  Home arrow Database Articles arrow Page 3 - Searching with Strings
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 
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
     
     
    ADVERTISEMENT


    Searching with Strings - Using FULLTEXT Searches continued


    (Page 3 of 4 )

    It’s possible to include the index definition in the initial CREATE TABLE statement, but it’s generally faster to create a nonindexed table and then add the index with ALTER TABLE after populating the table than to load a large dataset into an indexed table.

    To perform a search using the index, use MATCH() to name the indexed column and AGAINST() to specify what text to look for. For example, you might wonder, “How many times does the name Mizraim occur?” To answer that question, search the vtext column using this statement:

      mysql> SELECT COUNT(*) from kjv WHERE MATCH(vtext) AGAINST('Mizraim');
     
    +----------+
      | COUNT(*) |
      +----------+
      |        4 |
      +----------+

    To find out what those verses are, select the columns you want to see (the example here uses \G so that the results better fit the page):

      mysql> SELECT bname, cnum, vnum, vtext
         -> FROM kjv WHERE MATCH(vtext) AGAINST('Mizraim')\G 
      *************************** 1. row ***************************
      bname: Genesis
       cnum: 10
       vnum: 6
      vtext: And the sons of Ham; Cush, and Mizraim, and Phut, and Canaan.
      *************************** 2. row ***************************
      bname: Genesis
       cnum: 10
       vnum: 13
      vtext: And Mizraim begat Ludim, and Anamim, and Lehabim, and Naphtuhim,
      *************************** 3. row ***************************
      bname: 1 Chronicles
      
    cnum: 1
       vnum: 8
      vtext: The sons of Ham; Cush, and Mizraim, Put, and Canaan.
      *************************** 4. row ***************************
      bname: 1 Chronicles
       cnum: 1
       vnum: 11
     
    vtext: And Mizraim begat Ludim, and Anamim, and Lehabim, and Naphtuhim,

    The results come out in book, chapter, and verse number order in this particular instance, but that’s actually just coincidence. By default, FULLTEXT searches compute a relevance ranking and use it for sorting. To make sure a search result is sorted the way you want, add an explicit ORDER BY clause:

      SELECT bname, cnum, vnum, vtext
      FROM kjv WHERE MATCH(vtext) AGAINST('search string')
      ORDER BY bnum, cnum, vnum;

    If you want to see the relevance ranking, repeat the MATCH() ... AGAINST() expression in the output column list.

    More Database Articles Articles
    More By O'Reilly Media


     

    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 1 hosted by Hostway
    Stay green...Green IT