user1960836
user1960836

Reputation: 1782

Selecting all values that end with certain chars

I want to get a all the cars that end with a, c, d or f I can get a list of cars ending with a doing this:

select model 
from cars 
where model like "a%";

But I want a list of the models ending not only with a, but also with the other chars above:

I have tried doing this (which doesn't return correct list):

select model 
from cars 
where model like "%a"
or model like "%c"
or model like "%d"
or model like "%f";

This is also a very ugly attempt/solution. Imagine if I needed more models.

I am using mysql

Thanks for any help

Upvotes: 0

Views: 107

Answers (3)

Ritwik Bhar
Ritwik Bhar

Reputation: 284

Use regular expression to query $ means the end of String and ^ means the beginning

select model 
from cars 
where model REGEXP "(a|c|d|f)$";

If you wanted those characters in the beginning then you could have written:

select model 
from cars 
where model REGEXP "^(a|c|d|f)";

Upvotes: 1

morgb
morgb

Reputation: 2312

select model 
from cars
where substring(model, -1) IN ('a','c','d','f')

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269773

You can use regexp:

where model regexp '[acdf]$'

Upvotes: 3

Related Questions