Có, chúng ta có thể sử dụng tất cả chúng trong một truy vấn duy nhất. Trước hết, hãy tạo một bảng:
mysql> create table DemoTable ( StudentId int, StudentFirstName varchar(20), StudentLastName varchar(20), StudentAge int ); Query OK, 0 rows affected (0.53 sec)
Chèn các bản ghi vào bảng bằng lệnh insert:
mysql> insert into DemoTable values(100,'John','Smith',23); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(101,'Carol','Taylor',24); Query OK, 1 row affected (0.62 sec) mysql> insert into DemoTable values(103,'John','Doe',22); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(104,'Adam','Smith',23); Query OK, 1 row affected (0.18 sec)
Hiển thị tất cả các bản ghi từ bảng bằng câu lệnh select:
mysql> select * from DemoTable;
Điều này sẽ tạo ra đầu ra sau:
+-----------+------------------+-----------------+------------+ | StudentId | StudentFirstName | StudentLastName | StudentAge | +-----------+------------------+-----------------+------------+ | 100 | John | Smith | 23 | | 101 | Carol | Taylor | 24 | | 103 | John | Doe | 22 | | 104 | Adam | Smith | 23 | +-----------+------------------+-----------------+------------+ 4 rows in set (0.00 sec)
Truy vấn sau để sử dụng MySQL WHERE, AND & OR trong một truy vấn MySQL duy nhất để lấy các bản ghi:
mysql> select * from DemoTable where StudentFirstName='John' AND StudentLastName='Doe' OR StudentAge=26;
Điều này sẽ tạo ra đầu ra sau:
+-----------+------------------+-----------------+------------+ | StudentId | StudentFirstName | StudentLastName | StudentAge | +-----------+------------------+-----------------+------------+ | 103 | John | Doe | 22 | +-----------+------------------+-----------------+------------+ 1 row in set (0.00 sec)