使用 $ 符号代替 %

Use of $ symbol instead of %

我已经开始练习了SQL,我觉得我需要复习一些题目。

hackerrank 中的这个问题陈述指出,查询列出了 STATION 中不以元音结尾的城市名称。

我尝试使用通配符 '%[^aeiou]'

SELECT Distinct City 
FROM Station 
Where City LIKE '%[^aeiou]'
Order By City;

Compiler Message: Answer Wrong.

我知道执行程序的其他方法,但这个方法有什么问题。另外,我想知道 REGEXP '[^aiueo]$' 是如何工作的,但是 Like '%[^aeiou]Not Like '%[aeiou]' 是不可执行的?

LIKE 只支持通配符,你用它来进行非常简单的匹配。

REGEXPRLIKE 具有完整的正则表达式支持。

A regular expression is a powerful way of specifying a pattern for a complex search. This section discusses the functions and operators available for regular expression matching and illustrates, with examples, some of the special characters and constructs that can be used for regular expression operations.

参见 LIKE and on REGEXP

上的手册

如果你必须使用 LIKE 试试这个:

SELECT DISTINCT City 
FROM Station 
WHERE City NOT LIKE '%a'
OR City NOT LIKE '%e'
OR City NOT LIKE '%i'
OR City NOT LIKE '%o'
OR City NOT LIKE '%u';

如果您想要更快的查询,请使用 RIGHT () 或 REGEXP

MySQL 不支持 LIKE 的 SQL 服务器扩展。改用正则表达式:

SELECT DISTINCT City 
FROM Station 
WHERE City REGEXP '[^aeiou]$'
ORDER BY City;

请注意,您必须将正则表达式锚定到字符串的末尾。 LIKE 模式自动匹配整个字符串; REGEXP 模式可以匹配字符串中的任何位置,除非锚定到开头或结尾。

或者,避开正则表达式:

SELECT DISTINCT City 
FROM Station 
WHERE RIGHT(City, 1) NOT IN ('a', 'e', 'i', 'o', 'u')
ORDER BY City;