使用 OR 子句在 WHERE 语句中显示列及其值的 MySQL 查询


我们先创建一张表 -

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)

以下是使用 OR 子句在 WHERE 语句中返回所有列的查询 -

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)

更新于: 09-Sep-2019

60 次浏览

启动你的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.