如何利用单个 MySQL 查询检索多行数据?
若要利用单个 MySQL 查询检索多行数据,请使用 LIKE 运算符。首先,让我们创建一个表格 -
mysql> create table DemoTable1385 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.90 sec)
使用 insert 命令在表格中插入一些记录-
mysql> insert into DemoTable1385(Name) values('Chris Brown'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1385(Name) values('Adam Smith'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1385(Name) values('Carol Taylor'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1385(Name) values('John Doe'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable1385(Name) values('John Smith'); Query OK, 1 row affected (0.14 sec)
使用 select 语句显示表格中的所有记录 -
mysql> select * from DemoTable1385;
结果如下 -
+----+--------------+ | Id | Name | +----+--------------+ | 1 | Chris Brown | | 2 | Adam Smith | | 3 | Carol Taylor | | 4 | John Doe | | 5 | John Smith | +----+--------------+ 5 rows in set (0.00 sec)
以下查询可在单个 MySQL 查询中检索多行数据 -
mysql> select * from DemoTable1385 where Name LIKE '%Smith';
结果如下 -
+----+------------+ | Id | Name | +----+------------+ | 2 | Adam Smith | | 5 | John Smith | +----+------------+ 2 rows in set (0.00 sec)
广告