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

แสดงคอลัมน์และค่าด้วย OR ใน WHERE ของ MySQL

ใช้ OR รวมเงื่อนไขหลายอย่างใน WHERE

สร้างตารางนักเรียน

mysql> create table DemoTable777 ( 
    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    StudentName varchar(100), 
    StudentAge int ); 
Query OK, 0 rows affected (1.34 sec)

mysql> insert into DemoTable777(StudentName,StudentAge) values('Chris',23); Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable777(StudentName,StudentAge) values('Robert',21); Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable777(StudentName,StudentAge) values('Mike',19); Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable777(StudentName,StudentAge) values('Bob',22); Query OK, 1 row affected (0.19 sec)

คิวรีใช้ OR หลายเงื่อนไข

mysql> select * from DemoTable777 
where StudentId IN(1,2) OR StudentName='Mike' OR StudentAge=22;
+-----------+-------------+------------+
| StudentId | StudentName | StudentAge |
+-----------+-------------+------------+
| 1         | Chris       | 23         |
| 2         | Robert      | 21         |
| 3         | Mike        | 19         |
| 4         | Bob         | 22         |
+-----------+-------------+------------+
4 rows in set (0.00 sec)

ผลลัพธ์: ได้ทุกแถว เพราะแต่ละแถวตรงเงื่อนไขอย่างน้อยหนึ่งข้อ