I require a regular expression that selects a column from table, if it starts with a specific character sequence specified by the user.
example if i type in A i should get
Apple Apricot Acorn
if i type in A开发者_开发问答b
Abba Abdomen
etc..
This is for a query done in mysql 5.1
Thanks,
Don't use regex for this, use LIKE:
SELECT * from my_table WHERE name LIKE 'A%'
SELECT ... WHERE fieldname REGEXP '^Ab';
details here. But if you're doing purely "start of string" matching, then use .. LIKE 'Ab%'
, which is somewhat more efficient and can use indexes. If you need to search for any field which has a word anywhere in it that starts with Ab
, then by all means use regexes: REGEXP '[[:<:]Ab'
The regex portion of it would be:
'^yourCharacterSequenceHere'
精彩评论