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