MySQL 多列筛选?
要按多列执行筛选,请连同 OR 使用 where 子句。我们先创建一个表 −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(10), Score int ); Query OK, 0 rows affected (0.28 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable(Name,Score) values('John',80); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Name,Score) values('John',90); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Name,Score) values('Carol',89); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Name,Score) values('John',86); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Name,Score) values('Carol',98); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Name,Score) values('David',99); Query OK, 1 row affected (0.06 sec)
使用 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)
广告