MySQL 按多列进行过滤?
mysqlmysqli database更新于 2025/9/16 0:44:17
要按多列进行过滤,请使用 where 子句和 OR。让我们首先创建一个表 −
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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

