sql in not in_SQL IN – SQL NOT IN

sql in not in

SQL IN operator is used along with WHERE clause for providing multiple values as part of the WHERE clause.

SQL IN运算符与WHERE子句一起使用,以提供多个值作为WHERE子句的一部分。

1. SQL输入 (1. SQL IN)

SQL IN operator is almost like having multiple OR operators for the same column. Let’s discuss in detail about the SQL IN operator.

SQL IN运算符几乎就像在同一列上具有多个OR运算符一样。 让我们详细讨论一下SQL IN运算符。

There are two ways to define IN operator. We will discuss both the ways in details below.

定义IN运算符有两种方法。 我们将在下面详细讨论这两种方式。

1.1)作为IN的一部分的多个值 (1.1) Multiple values as part of IN)

Syntax:

语法

SELECT Column(s) FROM table_name WHERE column IN (value1, value2, ... valueN);

Using the above-mentioned syntax, we can define multiple values as part of IN operator.

使用上述语法,我们可以将多个值定义为IN运算符的一部分。

We will understand the above-mentioned syntax in more detail through some examples.

通过一些示例,我们将更详细地理解上述语法。

Let’s consider the following Student table for example purpose.

让我们考虑以下学生表格作为示例。

RollNoStudentNameStudentGenderStudentAgeStudentPercent
1GeorgeM1485
2MonicaF1288
3JessicaF1384
4TomM1178
卷号 学生姓名 学生性别 学生年龄 学生百分比
1个 乔治 中号 14 85
2 莫妮卡 F 12 88
3 杰西卡(Jessica) F 13 84
4 汤姆 中号 11 78

Scenario: Get the percentage of students whose age is 12 or 13.

方案 :获取12岁或13岁学生的百分比。

Query:

查询

SELECT StudentPercent FROM Student WHERE StudentAge IN ('12', '13');

Output:

输出

StudentPercent
88
84
学生百分比
88
84

1.2)选择查询作为IN的一部分 (1.2) Select Query as part of IN)

Syntax:

语法

SELECT Column(s) FROM table_name WHERE column IN (SELECT Statement);

Using the above-mentioned syntax, we can use SQL SELECT statement for providing values as part of the IN operator.

使用上述语法,我们可以使用SQL SELECT语句作为IN运算符的一部分来提供值。

We will understand the above-mentioned syntax in more detail through some examples. Let’s consider the following Product and Supplier table for example purpose.

通过一些示例,我们将更详细地理解上述语法。 让我们考虑以下“产品和供应商”表作为示例。

PRODUCT Table

产品表

ProductIdProductNameProductPrice
1Cookie10
2Cheese11
3Chocolate15
4Jam20
产品编号 产品名称 产品价格
1个 曲奇饼 10
2 起司 11
3 巧克力 15
4 果酱 20

SUPPLIER Table

供应商表

ProductIdProductNameSupplierName
1CookieABC
2CheeseXYZ
3ChocolateABC
4JamXDE
产品编号 产品名称 供应商名称
1个 曲奇饼 美国广播公司
2 起司 XYZ
3 巧克力 美国广播公司
4 果酱 XDE

Scenario: Get the price of the product where the supplier is ABC.

场景 :获取供应商为ABC的产品的价格。

Query:

查询

SELECT ProductPrice FROM Product WHERE ProductName IN 
    (SELECT ProductName FROM Supplier WHERE SupplierName = "ABC");

Output:

输出

ProductPrice
10
15
产品价格
10
15

1.3)SQL嵌套IN (1.3) SQL Nested IN)

We can also use IN inside other IN operator. To understand it better, let’s consider the below-mentioned scenario.

我们还可以在其他IN运算符内部使用IN。 为了更好地理解它,让我们考虑以下情况。

Scenario: Get the price of the product where the supplier is ABC and XDE.

场景 :获取供应商为ABC和XDE的产品的价格。

Query:

查询

SELECT ProductPrice FROM Product WHERE ProductName IN 
    (SELECT ProductName FROM Supplier WHERE SupplierName IN ( "ABC", "XDE" ));

Output

输出量

ProductPrice
10
15
20
产品价格
10
15
20

2. SQL NOT IN (2. SQL NOT IN)

SQL NOT IN operator is used to filter the result if the values that are mentioned as part of the IN operator is not satisfied. Let’s discuss in detail about SQL NOT IN operator.

如果不满足作为IN运算符一部分提到的值,则使用SQL NOT IN运算符过滤结果。 让我们详细讨论有关SQL NOT IN运算符。

Syntax:

语法

SELECT Column(s) FROM table_name WHERE Column NOT IN (value1, value2... valueN);

In the syntax above the values that are not satisfied as part of the IN clause will be considered for the result. Let’s consider the earlier defined Student table for example purpose.

在上面的语法中,将考虑将IN子句中不满足的值用作结果。 让我们以先前定义的Student表为例。

Scenario: Get the percentage of students whose age is not in 12 or 13.

方案 :获取12岁或13岁以下学生的百分比

Query

询问

SELECT StudentPercent FROM Student WHERE StudentAge NOT IN ('12', '13');

Output:

输出

StudentPercent
85
78
学生百分比
85
78

2.1)选择查询作为SQL NOT IN的一部分 (2.1) Select Query as part of SQL NOT IN)

Syntax:

语法

SELECT Column(s) FROM table_name WHERE column NOT IN (SELECT Statement);

Using the above-mentioned syntax, we can use SELECT statement for providing values as part of the IN operator. We will understand the above-mentioned syntax in more detail through some examples. Let’s consider the earlier defined Product and Supplier table for example purpose.

使用上述语法,我们可以使用SELECT语句作为IN运算符的一部分来提供值。 通过一些示例,我们将更详细地理解上述语法。 让我们考虑先前定义的“产品和供应商”表作为示例。

Scenario: Get the price of the product where the supplier is not ABC.

场景 :获取供应商不是ABC的产品的价格。

Query:

查询

SELECT ProductPrice FROM Product WHERE ProductName NOT IN 
    (SELECT ProductName FROM Supplier WHERE SupplierName = "ABC");

That’s all for SQL IN and SQL NOT IN operator examples.

这就是SQL IN和SQL NOT IN运算符示例的全部内容。

翻译自: https://www.journaldev.com/19165/sql-in-sql-not-in

sql in not in

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值