triq
triq

Reputation: 1977

regular expression for starting character sequence mysql 5.1

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 Ab

Abba Abdomen

etc..

This is for a query done in mysql 5.1

Thanks,

Upvotes: 1

Views: 778

Answers (3)

Jonathan M
Jonathan M

Reputation: 17451

The regex portion of it would be:

'^yourCharacterSequenceHere'

Upvotes: 0

Marc B
Marc B

Reputation: 360612

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'

Upvotes: 1

nobody
nobody

Reputation: 10645

Don't use regex for this, use LIKE:

SELECT * from my_table WHERE name LIKE 'A%'

Upvotes: 3

Related Questions