使用 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) -> ); Query OK, 0 rows affected (0.70 sec)
使用 insert 命令向表中插入一些记录。查询如下
mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Adam'); Query OK, 1 row affected (0.19 sec) mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Bob'); Query OK, 1 row affected (0.16 sec) mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('baden'); Query OK, 1 row affected (0.48 sec) mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Carol'); Query OK, 1 row affected (0.41 sec) mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Larry'); Query OK, 1 row affected (0.16 sec) mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Chris'); Query OK, 1 row affected (0.18 sec)
使用 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)
广告