» Birthday Bundle - Over $400 worth of Envato files for just $20

How to make an alphabetical search

Posted on August 30, 2008, Filled under PHP,  Bookmark it

This tutorial is aimed to help you how to make an alphabetical search using MySQL. Many sites use it these day and we will show you how do they do it.

Let’s suppose you have a business directory and you want to display the listings that are starting with a specific letter or a non-letter character (numeric, etc).

Let’s start with the non-alphabetic selection in case you need to select listings starting with a number or a non-alphanumeric character. Here’s the command:

SELECT * FROM `listings` WHERE Left(listing_name, 1) REGEXP '^[^a-z]+$';

The regular expressions are used here. The MySQL command selects all listings that have a first non-alphabetical character.

Now let’s move on with the alphabetical selection.

We will choose the letter ‘A’ for our example. Here’s the command that will select all listings starting with ‘A’.

SELECT * FROM `listings` WHERE Left(listing_name, 1) = 'a'; -- case insensitive

You can replace ‘A’ with any letter you wish.

Congratulations! Now you know how to perform an alphabetical search with MySQL.

Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!

Get our RSS Feed!

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)