IN()只要比较的字段或数据在给定的集合内,那么比较结果就为真,NOT IN()正好与IN()的功能相反。
1.准备
1 CREATE DATABASE mahaiwuji; 2 USE mahaiwuji; 3 CREATE TABLE emp 4 ( 5 empno INT PRIMARY KEY, 6 ename VARCHAR(10), 7 sal INT 8 ); 9 INSERT INTO emp VALUES (1,'smith',2800); 10 INSERT INTO emp VALUES (2,'allen',1500); 11 INSERT INTO emp VALUES (3,'ward',3500); 12 INSERT INTO emp VALUES (4,'jones',5000); 13 INSERT INTO emp VALUES (5,'martin',1600); 14 INSERT INTO emp VALUES (6,'blake',3000);
2.IN
查找sal为1500或3000或5000的信息
1 SELECT * FROM emp WHERE sal=1500 OR sal=3000 OR sal=5000; 2 -- 等价于 3 SELECT * FROM emp WHERE sal IN (1500,3000,5000);
3.NOT IN
查找sal不是1500也不是3000也不是5000的信息
1 SELECT * FROM emp WHERE sal!=1500 AND sal!=3000 AND sal<>5000; 2 -- 等价于 3 SELECT * FROM emp WHERE sal NOT IN (1500,3000,5000);
来源:https://www.cnblogs.com/mahaiwuji/p/12655766.html