选择以某些字符结尾的所有值

时间:2017-10-26 18:48:04

标签: mysql sql

我希望得到以a, c, d or f结尾的所有汽车 我可以得到以这样做结束的汽车列表:

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

但是我想要一个模型列表,不仅包括a,还包括上面的其他字符:

我尝试过这样做(它没有返回正确的列表):

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

这也是一个非常难看的尝试/解决方案。想象一下,如果我需要更多模型。

我正在使用mysql

感谢您的帮助

3 个答案:

答案 0 :(得分:3)

您可以使用regexp

where model regexp '[acdf]$'

答案 1 :(得分:1)

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

答案 2 :(得分:1)

使用正则表达式进行查询 $表示String的结尾,^表示开头

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

如果您想在开始时使用这些字符,那么您可以写下:

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