MySQL LIKE 运算符未按预期方式进行模式匹配

MySQL LIKE operator isn't working as expected for pattern matching

我得到了一个关系 table STATION,如下所示:

+------+----------------+-------+-------+--------+
| ID   | CITY           | STATE | LAT_N | LONG_W |
+------+----------------+-------+-------+--------+
|  794 | Kissee Mills   | MO    |   140 |     73 |
|  824 | Loma Mar       | CA    |    49 |    131 |
|  603 | Sandy Hook     | CT    |    72 |    148 |
|  478 | Tipton         | IN    |    34 |     98 |
|  619 | Arlington      | CO    |    75 |     93 |
|  711 | Turner         | AR    |    50 |    101 |
|  839 | Slidell        | LA    |    85 |    152 |
|  342 | Chignik Lagoon | AK    |   103 |    153 |
+------+----------------+-------+-------+--------+

我需要执行一个查询:

Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION.

我在 Ubuntu OS.MySQL 上使用 MySQL 5.7 版。

现在,这是我的尝试:

SELECT CITY
FROM STATION
WHERE LOWER(CITY) LIKE '[aeiou]%';

但这不会产生所需的结果。我得到一个空集作为输出。然而,这工作得很好:

SELECT CITY
FROM STATION
WHERE LOWER(CITY) REGEXP '^[aeiou]';

我想知道使用 LIKE 运算符的第一种方法有什么问题,知道吗?我是 SQL 的新手,所以如果我做了一些愚蠢的事情,请原谅。

感谢您的宝贵时间。

LIKE in MySQL 不支持通配符。你可以用正则表达式做类似的事情:

SELECT CITY
FROM STATION
WHERE LOWER(CITY) REGEXP '^[aeiou]';

LIKEREGEXP 不遵循相同的模式匹配规则。 REGEXP 支持正则表达式,而 LIKE 理解通配符 '_'(单个字符)和 '%'(任意数量的字符,包括所有字符)。

这在the pattern matching documentation中有很好的解释:

SQL pattern matching enables you to use _ to match any single character and % to match an arbitrary number of characters (including zero characters). In MySQL, SQL patterns are case-insensitive by default. Some examples are shown here. Do not use = or <> when you use SQL patterns. Use the LIKE or NOT LIKE comparison operators instead.

[...]

The other type of pattern matching provided by MySQL uses extended regular expressions. When you test for a match for this type of pattern, use the REGEXP_LIKE() function (or the REGEXP or RLIKE operators, which are synonyms for REGEXP_LIKE()).

旁注:默认情况下,两种模式匹配方法都不区分大小写(除非您使用一些特殊的排序规则),因此您的正则表达式可能会被简化为不使用 lower() 函数:

city like regexp '^[aeiou]'

如果用like来表达,那就是:

city like 'a%' 
or city like 'e%'
or city like 'i%'
or city like 'o%'
or city like 'u%'