最好使用IN()
而不是多个OR语句。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstName varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(FirstName) values('John'); mysql> insert into DemoTable(FirstName) values('Adam'); mysql> insert into DemoTable(FirstName) values('David'); mysql> insert into DemoTable(FirstName) values('Mike'); mysql> insert into DemoTable(FirstName) values('Bob');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Adam | | 3 | David | | 4 | Mike | | 5 | Bob | +----+-----------+ 5 rows in set (0.00 sec)
这是使用IN()
-的查询
mysql> select *from DemoTable where Id IN(2,3,5);
这将产生以下输出-
+----+-----------+ | Id | FirstName | +----+-----------+ | 2 | Adam | | 3 | David | | 5 | Bob | +----+-----------+ 3 rows in set (0.00 sec)