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!
- August 30, 2008
- article by Gabriel C.
- Leave a reply!
Sponsors
Related Posts
-
PHP: Make an Alphabetical Selection from Elements of an Arrayat October 5, 2008
-
PHP: Usage of Range() Function (Alphabetical & Numerical Output)at September 2, 2008
-
How to make a selection based on the ending valueat August 30, 2008
-
Highlight (Search) Key Words in a Text | PHPat August 30, 2008 with 8 comments
-
How to make a selection based on the beginning valueat August 30, 2008
