如何在 MySQL 中處理多個 AND 條件?
要在 MySQL 中使用多個 AND 條件,語法如下 −
select *from yourTableName where yourColumnName1=yourValue1 and yourColumnName2=yourValue2 and yourColumnName3=yourValue3;
我們先建立一個表 −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(20), StudentAge int, StudentCountryName varchar(40) ); Query OK, 0 rows affected (0.59 sec)
使用 insert 命令向表中插入一些記錄 −
mysql> insert into DemoTable(StudentName,StudentAge,StudentCountryName) values('John',23,'US');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(StudentName,StudentAge,StudentCountryName) values('Carol',21,'UK');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(StudentName,StudentAge,StudentCountryName) values('John',21,'US');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(StudentName,StudentAge,StudentCountryName) values('John',21,'AUS');
Query OK, 1 row affected (0.14 sec)使用 select 語句從表中顯示所有記錄 −
mysql> select *from DemoTable;
輸出
+-----------+-------------+------------+--------------------+ | StudentId | StudentName | StudentAge | StudentCountryName | +-----------+-------------+------------+--------------------+ | 1 | John | 23 | US | | 2 | Carol | 21 | UK | | 3 | John | 21 | US | | 4 | John | 21 | AUS | +-----------+-------------+------------+--------------------+ 4 rows in set (0.00 sec)
以下是 MySQL 中針對多個 AND 條件的查詢 −
mysql> select *from DemoTable where StudentName="John" and StudentAge=21 and StudentCountryName="AUS";
輸出
+-----------+-------------+------------+--------------------+ | StudentId | StudentName | StudentAge | StudentCountryName | +-----------+-------------+------------+--------------------+ | 4 | John | 21 | AUS | +-----------+-------------+------------+--------------------+ 1 row in set (0.00 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP