如何在MySQL的单个表中优化许多SELECT?

要优化许多SELECT,请使用一次并应用于IN()获取多个值。让我们首先创建一个表-

mysql> create table DemoTable
(
   Id int,
   Name varchar(100),
   Age int
);

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable values(1,'Chris',23);
mysql> insert into DemoTable values(2,'David',21);
mysql> insert into DemoTable values(3,'Mike',24);
mysql> insert into DemoTable values(4,'Robert',22);

使用select语句显示表中的所有记录-

mysql> select *from DemoTable ;

这将产生以下输出-

+------+--------+------+
| Id   | Name   | Age  |
+------+--------+------+
|    1 | Chris  |   23 |
|    2 | David  |   21 |
|    3 | Mike   |   24 |
|    4 | Robert |   22 |
+------+--------+------+
4 rows in set (0.00 sec)

以下是在单个表上优化许多SELECT的查询-

mysql> select Name from DemoTable where Age in(21,22,23);

这将产生以下输出-

+--------+
| Name   |
+--------+
| Chris  |
| David  |
| Robert |
+--------+
3 rows in set (0.00 sec)
猜你喜欢