murfitUK Posted August 4, 2006 Share Posted August 4, 2006 This should be so simple but my mind has gone blank and now I want to throw the computer out the window!I'm doing a "dictionary" which has two fields called "word" and "def".I want to do a list of words starting with letters A to E.This works:SELECT * FROM dict WHERE word LIKE 'A%' OR word LIKE 'B%' OR word LIKE 'C%' OR word LIKE 'D%' OR word LIKE 'E%';However, I am sure there is an easier way to do it, and I thought it was something like:SELECT * FROM dict WHERE word LIKE '[A-E]%';but it doesn't work. I've tried LIKE '{A-E}%' and '[AE]%' and every possible type of combination.Have I made this up? I'm sure I've seen it somewhere.Please help. Link to comment Share on other sites More sharing options...
skym Posted August 4, 2006 Share Posted August 4, 2006 Not sure, I don't have yet much experience with regular expressions, so it might be wrong but try:SELECT * FROM dict WHERE word REGEXP '^[A-E]';http://dev.mysql.com/doc/refman/4.1/en/regexp.html Link to comment Share on other sites More sharing options...
Jack McKalling Posted August 4, 2006 Share Posted August 4, 2006 (edited) What you are using is regular expressions in PERL, but that is what is used in PHP. The first example you show, like [A-E] should be like what you want in a PHP expression, but SQL doesn't work with PERL, at least not in the LIKE clause :)In SQL, the LIKE clause works with the sign % as a wildcard, and PERL uses a dot-asterisk for the same. That proves that LIKE doesn't uses PERL. What it does uses can be read here: http://www.mysql.org/doc/refman/5.0/en/str...-functions.htmlIt says it uses only % and _ for wildcards and no other syntax. It also refers to "REGEXP" which does uses regular expressions.Ha, skym had edited his post, he said it. Edited August 4, 2006 by Dan The Prof Link to comment Share on other sites More sharing options...
murfitUK Posted August 4, 2006 Author Share Posted August 4, 2006 Yeah - it works!!!!SELECT * FROM dict WHERE word REGEXP '^[A-E]';I knew there was a way to do it.Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now