Reputation: 2579
I have a table that has a column 'name' (varchar)
I want to get all the rows having a name that starts with 'a', 'b', 'c', 'd', 'e,' OR 'f'.
I've built the query as following:
WHERE LEFT(u.name, 1)='a' OR LEFT(u.name, 1)='b' OR LEFT(u.name, 1)='c' OR LEFT(u.name, 1)='d' OR LEFT(u.name, 1)='e' OR LEFT(u.name, 1)='f'
Is there a better way? using regular expressions maybe?
and let's say I had to do it for wider ranges (A to M) would it slow the query?
Upvotes: 1
Views: 5761
Reputation: 26353
There are almost too many ways to do this :) In addition to the other answers, any of these will work:
u.name >= 'a' AND u.name < 'g'
or
LEFT(u.name, 1) BETWEEN 'a' AND 'g'
or with a regex:
u.name REGEXP '^[a-f].*'
Upvotes: 3
Reputation: 263733
try using IN
WHERE LEFT(u.name, 1) IN ('a', 'b', 'c', 'd', 'e', 'f')
in this way you can define any starting character you want even if it's not in sequence.
Upvotes: 11