Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning rows where field's value starts with A, B or C

Status
Not open for further replies.

ingernet

Programmer
Joined
Feb 5, 2001
Messages
68
Location
US
Hello all,

Happy Christmas Eve.

I'm trying to pull all the last names that start with A-C, D-F, G-H, etc. I've tried to use

select * from mytable where LastName like '[ABC]%'

...but while it makes perfect sense to me, MySQL is having none of it. I'm sure the syntax is off, but the manual isn't offering anything clear-cut as an alternative.

Anyone have any ideas?

Thanks,
Inger
 
There are several ways to do this.
Code:
SELECT *
FROM mytable
WHERE SUBSTR(LastName,1,1) BETWEEN 'A' AND 'C'

or

Code:
SELECT *
FROM mytable
WHERE LastName LIKE 'A%' 
OR LastName LIKE 'B%' 
OR LastName LIKE 'C%'
These are fairly basic queries. May I suggest that you read a good book on MySQL?

Andrew
Hampshire, UK
 
You could also use:
[tt]
WHERE lastname REGEXP '^[abc]'
# (or RLIKE instead of REGEXP)
[/tt]
However, this is likely to be slower than towerbase's 2nd solution, as it won't use any indexes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top