使用 AND 和 OR 運算子來返回多行的 MySQL 查詢
我們先建立一個表 -
mysql> create table DemoTable
(
StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
StudentName varchar(40),
StudentMathMarks int,
StudentMySQLMarks int,
status ENUM('ACTIVE','INACTIVE')
);
Query OK, 0 rows affected (0.47 sec)用 insert 命令在表中插入一些記錄 -
mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Chris',45,67,'active');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Bob',89,78,'inactive');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('David',56,68,'active');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Robert',68,75,'active');
Query OK, 1 row affected (0.18 sec)使用 select 語句從表中顯示所有記錄 -
mysql> 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 運算子返回多個行記錄的查詢 -
mysql> 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)
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP