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

ตัวดำเนินการ NOT IN ใน MySQL แทนหลายเงื่อนไข !=

ใช้ NOT IN แทนการเขียน != ... AND != ... หลายตัว ไวยากรณ์:

SELECT * FROM yourTableName WHERE yourColumnName NOT IN(1,2,7);

สร้างตาราง:

mysql> create table User_informations ( UserId int, UserName varchar(20) ); Query OK, 0 rows affected (0.47 sec)

แทรกข้อมูล 8 แถว...

แสดงข้อมูล:

mysql> select * from User_informations;

ผลลัพธ์:

+--------+----------+ | UserId | UserName | +--------+----------+ | 12 | Maxwell | | 7 | David | | 1 | Ramit | | 10 | Bob | | 2 | Carol | | 14 | Sam | | 6 | Mike | | 4 | Robert | +--------+----------+ 8 rows in set (0.00 sec)

ใช้ NOT IN แทน UserId != 1 AND UserId != 2 AND UserId != 7:

mysql> select * from User_informations where UserId NOT IN(1,2,7);

ผลลัพธ์

+--------+----------+ | UserId | UserName | +--------+----------+ | 12 | Maxwell | | 10 | Bob | | 14 | Sam | | 6 | Mike | | 4 | Robert | +--------+----------+ 5 rows in set (0.00 sec)