MySQL 查询使用 WHERE 语句中的 OR 显示列及其值
mysqlmysqli database更新于 2025/11/7 21:32:17
首先我们创建一个表 −
mysql> create table DemoTable777 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(100), StudentAge int ); Query OK, 0 rows affected (1.34 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable777(StudentName,StudentAge) values('Chris',23);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable777(StudentName,StudentAge) values('Robert',21);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable777(StudentName,StudentAge) values('Mike',19);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable777(StudentName,StudentAge) values('Bob',22);
Query OK, 1 row affected (0.19 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable777;
这将产生以下输出 -
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Chris | 23 | | 2 | Robert | 21 | | 3 | Mike | 19 | | 4 | Bob | 22 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
以下是在 WHERE 语句中使用 OR 返回所有列的查询 −
mysql> select *from DemoTable777 where StudentId IN(1,2) OR StudentName='Mike' OR StudentAge=22;
这将产生以下输出 -
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Chris | 23 | | 2 | Robert | 21 | | 3 | Mike | 19 | | 4 | Bob | 22 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

