不是以元音开头,也不是以元音结尾

时间:2018-08-20 20:50:44

标签: sql sql-server

我正在Hackerrank上进行挑战,问题是表'STATION'中所有城市的返回列表,这些城市中的城市不以元音开头,也不以元音结尾。

此查询无效:

select distinct CITY from STATION where CITY NOT LIKE '[A,E,I,O,U]% 
[a,e,i,o,u]'

但是此查询确实有效:

select distinct CITY from STATION where CITY NOT LIKE '[A,E,I,O,U]%' 
AND CITY NOT LIKE '%[a,e,i,o,u]'

任何人都可以帮助我理解为什么第一个查询不起作用吗?

5 个答案:

答案 0 :(得分:4)

您的第二个查询工作正常-因为逗号不会出现在名称的开头或结尾。

极有可能。理想的解决方案是单一模式:

where city like '[^aeiou]%[^aeiou]'

答案 1 :(得分:3)

select distinct city
from station
where city rlike '^[^aeiouAEIOU].*' and city rlike '^.*[^aeiouAEIOU]$'

答案 2 :(得分:0)

尝试一下:

    SELECT DISTINCT CITY  FROM STATION  WHERE UPPER(SUBSTR(CITY, 1, 1)) NOT IN ('A', 'E', 'I', 'O', 'U')
     AND UPPER(SUBSTR(C.CITY, LENGTH(CITY), 1)) NOT IN ('A', 'E', 'I', 'O', 'U')

答案 3 :(得分:0)

尝试使用MySQL解决方案:

select distinct CITY from STATION where substr(CITY, 1, 1) not in ('a','e','i','o','u') and substr(CITY, -1, 1) not in ('a','e','i','o','u');

答案 4 :(得分:0)

lightSelector