Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

ใช้ WHERE, AND, OR ในคำสั่ง MySQL เดียวกันได้หรือไม่

ได้ ใช้ร่วมกันได้ โดยมีลำดับความสำคัญ: AND ก่อน OR ใช้วงเล็บเพื่อกำหนดลำดับชัดเจน

ตัวอย่าง

mysql> create table DemoTable (
    StudentId int,
    StudentFirstName varchar(20),
    StudentLastName varchar(20),
    StudentAge int
);
Query OK, 0 rows affected (0.53 sec)
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)

คิวรี่รวม WHERE, AND, OR

mysql> select * from DemoTable where StudentFirstName='John' AND StudentLastName='Doe' OR StudentAge=26;

ผลลัพธ์

+-----------+------------------+-----------------+------------+
| StudentId | StudentFirstName | StudentLastName | StudentAge |
+-----------+------------------+-----------------+------------+
| 103       | John             | Doe             | 22         |
+-----------+------------------+-----------------+------------+

เงื่อนไข (FirstName='John' AND LastName='Doe') OR Age=26 ตรงกับแถวที่ 3