MySQL
  Home arrow MySQL arrow Page 6 - Optimizing Queries with Operators, Branching and Functions, continued
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
MYSQL

Optimizing Queries with Operators, Branching and Functions, continued
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2006-04-06


    Table of Contents:
  • Optimizing Queries with Operators, Branching and Functions, continued
  • Trigonometric Functions
  • Other Math Functions
  • Conversion of Numbers Between Bases
  • String Functions and Regular Expressions
  • Pattern-Matching and Regular Expressions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Optimizing Queries with Operators, Branching and Functions, continued - Pattern-Matching and Regular Expressions
    ( Page 6 of 6 )

    An often-overlooked feature of MySQL is its ability to perform pattern-matching, including the use of using regular expressions. For simple pattern-matching, the LIKE operator is used, with the following syntax:

    string LIKE pattern

    This operator yields either a 1 (TRUE) or 0 (FALSE), depending on whether or not a match for pattern is found in string. The pattern argument supports two wildcards:

    • _ (underscore character), for any single character
    • % (percent sign), for any group of characters

    These wildcards can be used at the beginning, end, or anywhere inside of pattern. Here are some examples:


    NOTE
      Unlike some other databases, MySQL does not support the grouping ([ ]) or negation (^) operators for use with the LIKE
    operator.

    MySQL also supports full-fledged regular expressions, similar to that implemented in a number of programming languages (using the POSIX 1003.2
    standard syntax). A regular expression is a sequence of characters and/or special characters (known as pattern modifiers) that forms a pattern for which a match is sought in a string. In MySQL, you test for a match using the REGEXP operator
    and this syntax:

    string REGEXP pattern

    This will evaluate as true if a match is found, and false if it is not.

    Regular expressions as used in MySQL accept the pattern modifiers shown in Table 4-2.

    Table 4-2. MySQL Regular Expression Pattern Modifiers

    *

    MODIFIER*

    DESCRIPTION

    ^

    Outside any group, marks the beginning of the string to be searched. Within a group, negates the pattern; for example, [^a-e] matches any character except the letters a, b, c, d, or e

    $

    End of the string to be searched

    .

    Matches any single character

    *

    Zero or more occurrences of the preceding group (equivalent to {0, })

    Table 4-2. MySQL Regular Expression Pattern Modifiers (Continued)

    MODIFIER*

    DESCRIPTION

    +

    One or more occurrences of the preceding group (equivalent to {1, })

    ?

    Zero or one occurrence of the preceding group (equivalent to {0, 1})

    |

    Branch operator (OR); for example, dog|cat matches either dog or cat

    ()

    Encloses an expression; for example, (aeiou) matches the sequence aeiou

    []

    Encloses a set or range; for example, [1-4] matches any one of the digits 1, 2, 3, or 4; [c-g] matches any one of the letters c, d, e, f, or g; [aeiou] matches any one of the letters a, e, i, o, or u. It is possible to combine sets and ranges in a single group; for example, [0-4g] matches any one of the following: 0g, 1g, 2g, 3g, or 4g.

    {}

    Quantifies the preceding group; {N} indicates that the group must be repeated N times; {N, M} indicates that the group may be repeated from N to M times.

    * To match a literal occurrence of any of the characters shown in the Modifier column, escape it with a backslash; for example, [why\?] matches the string “why?” (including the question mark). To match a backslash, use a double backslash: \\ .

    The following are some examples of regular expressions.

    MySQL doesn’t provide any direct means to return matched expressions (only that a match was or wasn’t found), but it’s still possible to use REGEXP in WHERE clauses to achieve the same effect, as in the next example.

    The RLIKE operator is also supported as an alias to REGEXP , and it works in the same way.


    TIP  
    For complete details on using regular expressions, see the Unix regexp(7) man page. If you’re a Windows user, you can find this on the Web at http://www.unusualresearch.com/regex/ regexmanpage.htm.

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



     
     
    >>> More MySQL Articles          >>> More By Apress Publishing
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT