SQL中in和not in

Usage of in in SQL

IN和NOT IN

Both IN and NOT IN belong to a certain set;
IN is used to find tuples whose attribute values ​​belong to the specified set;
NOT IN is used to find tuples whose attribute values ​​do not belong to the specified set.

IN

Example: Query the names and genders of students in the Department of Computer Science (CS), Department of Mathematics (MA), and Department of Information (IS).

select Sname,Ssex
from Student 
Where Sdept in('CS','MA','IS');

IN in some questions can be replaced by multiple ORs. For example, this question can also be written in this form:

select Sname,Ssex
From Student 
where Sdept='CS' or Sdept='MA' or Sdept='IS';

Example PTA-R10-42 Query one of all the information of the two books titled "C language programming" and "VB programming" in the book table

select *
from 图书
where 书名 in ('C语言程序设计','VB程序设计');
#*代表所有信息,也可一一列出
#select 条形码,书名,作者,出版社,出版日期,售价

Example PTA-R10-43 Query all information of two readers of accounts D002 and D003 in the reader table

select *
from 读者
where 账号 in ('D002','D003');

Example PTA-R10-47 Query all information of the male employee surnamed Chen in the employee table

select *
from 员工
where 姓名 like "陈%" and 性别 in ('男')#也可以是:where 姓名 like "陈%" and 性别 = ('男');
#或者 having 姓名 like "陈%" and 性别 in ('男');

Example PTA-R10-48 Query all information of Chen Chengrui and Zhong Ming in the employee table

select *
from 员工
where 姓名 in ("陈诚瑞","钟鸣");

Example PTA-R10-49 Query the order information undertaken by employee No. 011 and employee No. 121 in the order table

select *
from 订单
where 员工编号 in('011','121');

Example PTA-R10-50 A1-2 Find order information according to the country where it is located

select OrderID,CustomerID
from orders
where ShipCountry in ('Germany','Brazil','France');

NOT IN
Example: Inquire about the names and genders of students who are neither in the third-level science department, mathematics department, nor information department.

select Sname,Ssex
from Student
where Sdept not in ('CS','MA','IS');

Example: PTA-R10-51 A1-6 Find out the customer information in the customer table that is not a specific city

select CustomerID,Phone
from customers
where City not in ('Madrid','Torino','Paris');

Guess you like

Origin blog.csdn.net/weixin_45867259/article/details/121181343