MySQL Select Based on first letter

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
ILoveJackDaniels
Forum Commoner
Posts: 43
Joined: Mon May 20, 2002 8:18 am
Location: Brighton, UK

MySQL Select Based on first letter

Post by ILoveJackDaniels »

I've got a database of users and want to create a members list. I've set it up so that a member can click on, say, "A" for all usernames beginning with an "A". However, I have quite a lot of members whose names begin with a digit or non-alphanumeric character. How do I form a select to grab all data where a username's first character is not alphanumeric?

My current query for selecting names starting with "A" reads something like this:

Code: Select all

SELECT * FROM users WHERE username like "A%"
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

A little more tricky:

Code: Select all

SELECT * FROM users WHERE SUBSTRING(username,1,1) NOT REGEXP "їa-zA-Z]"
Not tested, but I think it should work.

Kelvin
User avatar
ILoveJackDaniels
Forum Commoner
Posts: 43
Joined: Mon May 20, 2002 8:18 am
Location: Brighton, UK

Post by ILoveJackDaniels »

Spot on, releasedj, thankyou.
Post Reply