使用MySQL查询查找所有以字母“ a”,“ b”或“ c”开头的名称?
您需要使用带有OR运算符的LIKE来查找以a或b或c开头的所有名称。语法如下:
SELECT *FROM yourTableName WHERE yourColumnName like 'A%' or yourColumnName like 'B%' or yourColumnName like 'C%';
上面的查询查找所有仅以字母“a”或“b”或“c”开头的名称。为了理解上述语法,让我们创建一个表。创建表的查询如下:
mysql> create table AllNamesStartWithAorBorC -> ( -> Id int NOT NULL AUTO_INCREMENT, -> EmployeeName varchar(20), -> PRIMARY KEY(Id) -> );
使用insert命令在表中插入一些记录。查询如下:
mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Adam'); mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Bob'); mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('baden'); mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Carol'); mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Mike'); mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Larry'); mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Chris');
使用select语句显示表中的所有记录。查询如下:
mysql> select *from AllNamesStartWithAorBorC;
以下是输出:
+----+--------------+ | Id | EmployeeName | +----+--------------+ | 1 | Adam | | 2 | Bob | | 3 | baden | | 4 | Carol | | 5 | Mike | | 6 | Larry | | 7 | Chris | +----+--------------+ 7 rows in set (0.00 sec)
这是查找以a或b或c开头的名称的查询。查询如下:
mysql> select *from AllNamesStartWithAorBorC where EmployeeName like 'A%' or EmployeeName like 'B%' or -> EmployeeName like 'C%';
以下是输出:
+----+--------------+ | Id | EmployeeName | +----+--------------+ | 1 | Adam | | 2 | Bob | | 3 | baden | | 4 | Carol | | 7 | Chris | +----+--------------+ 5 rows in set (0.00 sec)