要按多列执行过滤,请使用where子句和OR。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(10), Score int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Name,Score) values('John',80); mysql> insert into DemoTable(Name,Score) values('John',90); mysql> insert into DemoTable(Name,Score) values('Carol',89); mysql> insert into DemoTable(Name,Score) values('John',86); mysql> insert into DemoTable(Name,Score) values('Carol',98); mysql> insert into DemoTable(Name,Score) values('David',99);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-------+-------+ | Id | Name | Score | +----+-------+-------+ | 1 | John | 80 | | 2 | John | 90 | | 3 | Carol | 89 | | 4 | John | 86 | | 5 | Carol | 98 | | 6 | David | 99 | +----+-------+-------+ 6 rows in set (0.00 sec)
以下是执行多列过滤的查询-
mysql> select *from DemoTable where (Name,Score)=('John', 90) or (Name,Score)=('Carol', 98);
这将产生以下输出-
+----+-------+-------+ | Id | Name | Score | +----+-------+-------+ | 2 | John | 90 | | 5 | Carol | 98 | +----+-------+-------+ 2 rows in set (0.00 sec)