Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > SQL Wish List | Report Bug | About Me ]

9.3.17     Simple text searching using LIKE

This is NOT the latest copy of this book; click here for the latest version.

Consider this SQL statement:

SELECT * FROM usertable WHERE FirstName = 'Paul' OR FirstName = 'Paula' OR FirstName = 'Pauline';

Here we're trying to match the FirstName field against several possible variants of names beginning with "Paul" - there are probably more variants, in which case the query would need to be longer. There is a much better alternative available in the LIKE operator - it allows simple pattern matching to be performed on each row, and therefore allows you to be more flexible in your searches.

For example, the % sign means "any number of characters" (including no characters), which means we could replace the above query with this:

SELECT * FROM usertable WHERE FirstName LIKE 'Paul%';

That would match all names starting with "Paul", case insensitive. We can even use "%Paul%" to match Paul, Paula, John Paul, etc - any number of characters, followed by "Paul", followed by any number of characters.

There is also the _ sign, which means "match any single character", which means that "p_p" matches PHP, pup, pip, pop, and even p_p - it matches everything including itself. Both of these symbols, % and _, can be matched against if you escape them. For example, if you are looking to match any number of characters followed by %hello%, you would need this query:

SELECT * FROM usertable WHERE FirstName LIKE '%\%hello\%%';

Note that _ and % do not match NULL.

One important thing to note is that the LIKE operator will attempt to use any indexes you have on the field, however the index is ignored when you use queries that begin with a wild card - Paul% is much faster than %Paul%.





<< 9.3.16 Managing indexes   9.3.18 Advanced text searching using full-text indexes >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Gus Jones - 30 Aug 2008

This chapter on SQL is a pleasure to read - everything is made crystal clear.

Gus Jones - 30 Aug 2008

This chapter on SQL is a pleasure to read - everything is made crystal clear.

Gus Jones - 30 Aug 2008

This chapter on SQL is a pleasure to read - everything is made crystal clear.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus two?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow