具有多个值的通配符?

时间:2015-10-02 11:58:37

标签: sql

我得到了这些Challenge on HackerRank

  

编写查询以打印以元音开头的CITY列表(a,e,   我,o,u)按字典顺序排列。不要打印重复项。

我的解决方案:

Select DISTINCT(City)
From Station
Where City like 'A%' 
   or City like 'E%' 
   or City like 'I%' 
   or City like 'O%' 
   or City like 'U%'
Order by City;

其他解决方案:

select distinct(city) from station
where upper(substr(city, 1,1)) in ('A','E','I','O','U');

这非常聪明,但我想知道,有没有其他方法可以解决这个问题?

任何类型的数据库都可以。

3 个答案:

答案 0 :(得分:1)

MySQL / MariaDB中的正则表达式:

select distinct city from station where city regexp '^[aeiouAEIOU]' order by city

答案 1 :(得分:0)

在SQLserver中您可以使用类似注册的语法:

select distinct city from station where city like '[aeuio]%' Order by City

答案 2 :(得分:0)

在SQL Server中

select distinct city from station 
 where charindex ('a',CITY) =1
    or charindex ('e',CITY) =1
    or charindex ('i',CITY) =1
    or charindex ('o',CITY) =1
    or charindex ('u',CITY) =1