Prodigal Son Posted August 19, 2009 Share Posted August 19, 2009 I have a table of data that I'm trying to fetch out alphabetically from. Sort of like a A-Z list where you click a letter and results starting with that letter are shown. So I have: SELECT * FROM table WHERE column LIKE 'a%' for items that start with a. My first question is that is there any difference from using SELECT * FROM table WHERE SUBSTRING(column,1,1) = 'a' I checked the explain and they both looked the same. But it seems like substring is faster? Does one use less resources or is faster? My second question is how can I fetch results that don't start with a-z. The closest I've gotten is: SELECT * FROM table WHERE column NOT BETWEEN 'A' AND 'Z' but that will also show results starting with Z. So if I do SELECT * FROM table WHERE column NOT BETWEEN 'A' AND 'Z' AND column NOT LIKE 'Z%' it'll work, but I'm sure there would be a better way to do it? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/170992-solved-selecting-by-letter-and-non-alphabetical-letter/ Share on other sites More sharing options...
TeNDoLLA Posted August 19, 2009 Share Posted August 19, 2009 You could use this for matching non alphabets SELECT nick FROM users WHERE nick NOT REGEXP '^[a-z]' Quote Link to comment https://forums.phpfreaks.com/topic/170992-solved-selecting-by-letter-and-non-alphabetical-letter/#findComment-901888 Share on other sites More sharing options...
Prodigal Son Posted August 19, 2009 Author Share Posted August 19, 2009 You could use this for matching non alphabets SELECT nick FROM users WHERE nick NOT REGEXP '^[a-z]' Thanks, that works great. Didn't know you could use regexp with mysql. Anyone have a suggestion for using LIKE vs SUBSTRING? Or would use either one be fine? Quote Link to comment https://forums.phpfreaks.com/topic/170992-solved-selecting-by-letter-and-non-alphabetical-letter/#findComment-901975 Share on other sites More sharing options...
corbin Posted August 19, 2009 Share Posted August 19, 2009 If you have an index on the field, LIKE 'a%' should be faster. Quote Link to comment https://forums.phpfreaks.com/topic/170992-solved-selecting-by-letter-and-non-alphabetical-letter/#findComment-902129 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.