查找不以元音开头的城市名称列表的 MySQL 查询?


您可以使用 DISTINCT 和 RLIKE 运算符来查找不以元音开头的城市名称列表。

语法如下:

SELECT DISTINCT yourCityColumnName FROM yourTableName WHERE
yourCityColumnName NOT RLIKE ‘ ^[AEIOUaeiou].*$’;

为了理解上述语法,让我们创建一个表。这里,我们有一列用于城市名称。

创建表的查询如下:

mysql> create table Employee_Information
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> EmployeeName varchar(20),
   -> CityName varchar(20),
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.76 sec)

使用 INSERT 命令在表中插入一些记录。查询如下:

mysql> insert into Employee_Information(EmployeeName,CityName) values('Larry','New York');
Query OK, 1 row affected (0.12 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Sam','Indianapolis');
Query OK, 1 row affected (0.15 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Carol','El Paso');
Query OK, 1 row affected (0.16 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('John','Austin');
Query OK, 1 row affected (0.07 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Mike','Denver');
Query OK, 1 row affected (0.06 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('David','Las Vegas');
Query OK, 1 row affected (0.11 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('James','Albuquerque');
Query OK, 1 row affected (0.13 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Robert','Portland');
Query OK, 1 row affected (0.28 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Richard','Irvine');
Query OK, 1 row affected (0.10 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Michael',' Garland');
Query OK, 1 row affected (0.13 sec)

使用 select 语句显示表中的所有记录。查询如下:

mysql> select *from Employee_Information;

以下是输出:

+----+--------------+--------------+
| Id | EmployeeName | CityName     |
+----+--------------+--------------+
|  1 | Larry        | New York     |
|  2 | Sam          | Indianapolis |
|  3 | Carol        | El Paso      |
|  4 | John         | Austin       |
|  5 | Mike         | Denver       |
|  6 | David        | Las Vegas    |
|  7 | James        | Albuquerque  |
|  8 | Robert       | Portland     |
|  9 | Richard      | Irvine       |
| 10 | Michael      | Garland      |
+----+--------------+--------------+
10 rows in set (0.00 sec)

以下是如何查找不以元音开头的城市名称列表的查询。这意味着城市名称的第一个字母不能以 A、E、I、O、U 或 a、e、i、o、u 开头:

mysql> SELECT DISTINCT CityName FROM Employee_Information
   -> WHERE CityName NOT RLIKE '^[AEIOUaeiou].*$';

以下是输出:

+-----------+
| CityName  |
+-----------+
| New York  |
| Denver    |
| Las Vegas |
| Portland  |
| Garland   |
+-----------+
5 rows in set (0.00 sec)

更新于:2020年6月30日

12K+ 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告