Select like number

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
doeboy
Forum Newbie
Posts: 10
Joined: Sun Mar 21, 2004 10:18 am

Select like number

Post by doeboy »

I have a query that allows users to seach by the first letter of a list. I would like for them to search by numbers if there are any in the list.

Current code:

SELECT FROM WHERE field LIKE '$letter%'

The letter is very easy (because it is one letter) There are 10 numbers (0-9).

Any thoughts?
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

I don't really understand what you're asking... There are 26 Letters? Are you looking to search for a whole number. For instance, if 123 is on the database you want to search for 123 and not get 12? Let us know some more information. Maybe how you have it laid our and we can help you.
doeboy
Forum Newbie
Posts: 10
Joined: Sun Mar 21, 2004 10:18 am

Post by doeboy »

No. So I have a list of names. Users can filter the list based on letters. So show all names that start with the letter c.

I have that part down. However I have another list where they might be needing to filter by numbers. So rather than clicking on A-Z, they could click on a # sign and the list would be filtered to show only results that start with a number.
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

Oh ok that makes sense now.

This is the only way I can come up with how to do what you want right now. There is probably a better way but this is all I could come up with right now and this should work for you.

Code: Select all

SELECT foo FROM bar WHERE foo LIKE '1%' or '2%' or '3%' or '4%' or '5%' or '6%' or '7%' or '8%' or '9%' or '0%'
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

here is another way:

Code: Select all

SELECT foo FROM bar WHERE left(foo,1) between '0' and '9'
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

ahh I was trying to think how to do that Weirdan. I knew there was an easier way.
Post Reply