这是来自 HackerRank 的挑战问题.
我的第一个查询工作正常:
select distinct city
from station
where city not regexp '^[aeiou]'
and city not regexp '[aeiou]$'
但是我的第二个查询给出了错误的答案
select distinct city
from station
where city not regexp '^[aeiouAEIOU].*[aeiouAEIOU]$'
正在编译,但给出了错误的结果
最佳答案
https://dev.mysql.com/doc/refman/8.0/en/regexp.html
“NOT REGEXP”是 REGEXP 的否定,
'REGEXP' -> 检查 'REGEXP' 如果满足则返回 1,否则返回 0
select 'Chelsea' REGEXP '^[aeiou].*[aeiou]$' ;
0
“NOT REGEXP” -> 检查“REGEXP”,然后返回结果的相反值。
select 'Chelsea' not REGEXP '^[aeiou].*[aeiou]$' ;
1
在本例中,“Chelsea”不以元音开头,因此 REGEXP
为 0,not REGEXP
为 1。
因此,您正在计算城市,甚至名称以元音开头或结尾。
关于mysql - 从 STATION 查询不以元音开头且不以元音结尾的 CITY 名称列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56575274/