您可以将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) -> );
使用INSERT命令在表中插入一些记录。查询如下-
mysql> insert into Employee_Information(EmployeeName,CityName) values('Larry','New York'); mysql> insert into Employee_Information(EmployeeName,CityName) values('Sam','Indianapolis'); mysql> insert into Employee_Information(EmployeeName,CityName) values('Carol','El Paso'); mysql> insert into Employee_Information(EmployeeName,CityName) values('John','Austin'); mysql> insert into Employee_Information(EmployeeName,CityName) values('Mike','Denver'); mysql> insert into Employee_Information(EmployeeName,CityName) values('David','Las Vegas'); mysql> insert into Employee_Information(EmployeeName,CityName) values('James','Albuquerque'); mysql> insert into Employee_Information(EmployeeName,CityName) values('Robert','Portland'); mysql> insert into Employee_Information(EmployeeName,CityName) values('Richard','Irvine'); mysql> insert into Employee_Information(EmployeeName,CityName) values('Michael',' Garland');
使用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)