MySQL查询使用AND&OR运算符返回多行记录

让我们首先创建一个表-

create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(40),
   StudentMathMarks int,
   StudentMySQLMarks int,
   status ENUM('ACTIVE','INACTIVE')
);

使用插入命令在表中插入一些记录-

insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Chris',45,67,'active');
insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Bob',89,78,'inactive');
insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('David',56,68,'active');
insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Robert',68,75,'active');

使用select语句显示表中的所有记录-

select *from DemoTable;

这将产生以下输出-

+-----------+-------------+------------------+-------------------+----------+
| StudentId | StudentName | StudentMathMarks | StudentMySQLMarks | status   |
+-----------+-------------+------------------+-------------------+----------+
|         1 | Chris       |               45 |                67 | ACTIVE   |
|         2 | Bob         |               89 |                78 | INACTIVE |
|         3 | David       |               56 |                68 | ACTIVE   |
|         4 | Robert      |               68 |                75 | ACTIVE   |
+-----------+-------------+------------------+-------------------+----------+
4 rows in set (0.00 sec)

以下是使用AND&OR运算符返回多个行记录的查询-

select *from DemoTable
   where status='active'
and (StudentMathMarks=68 or StudentMySQLMarks=67);

这将产生以下输出-

+-----------+-------------+------------------+-------------------+--------+
| StudentId | StudentName | StudentMathMarks | StudentMySQLMarks | status |
+-----------+-------------+------------------+-------------------+--------+
|         1 | Chris       |               45 |                67 | ACTIVE |
|         4 | Robert      |               68 |                75 | ACTIVE |
+-----------+-------------+------------------+-------------------+--------+
2 rows in set (0.00 sec)