sql 逻辑运算符不等于

This article explores the SQL Not Equal comparison operator (<>) along with its usage scenarios.

本文探讨了SQL不等于比较运算符(<>)及其使用方案。

介绍 (Introduction)

We must have used comparison operators in mathematics in the early days. We use these operators to compare different values based on the conditions. For example, we might compare the performance of two authors based on a number of articles. Suppose Raj wrote 85 articles while Rajendra wrote 100 articles. We can say that-

在早期,我们必须在数学中使用比较运算符。 我们使用这些运算符根据条件比较不同的值。 例如,我们可能会根据大量文章比较两位作者的表现。 假设Raj写了85篇文章,而Rajendra写了100篇文章。 我们可以说-

The total number of articles written by Rajendra > (Greater than) the total number of articles written by Raj.

Rajendra撰写的文章总数> (大于) Rajend撰写的文章总数。

We can have the following comparison operators in SQL.

我们可以在SQL中使用以下比较运算符。

Operator

Description

=

Equals to

<>

Not Equal

!=

Not Equal

>

Greater than

>=

Greater than to equals to

<

Less than

<=

Less than or equals to

操作员

描述

=

等于

<>

不平等

!=

不平等

>

比...更棒

> =

大于等于

<

少于

<=

小于或等于

In the table above, we can see that there are two operators for Not Equal (<> and !=) . In this article, we will explore both operators and differences in these as well.

在上表中,我们可以看到不等于(<>和!=)有两个运算符。 在本文中,我们将同时探讨运算符和它们之间的差异。

SQL不等于<>比较运算符 (SQL Not Equal <> Comparison Operator)

We use SQL Not Equal comparison operator (<>) to compare two expressions. For example, 10<>11 comparison operation uses SQL Not Equal operator (<>) between two expressions 10 and 11.

我们使用SQL不等于比较运算符(<>)比较两个表达式。 例如,10 <> 11比较操作在两个表达式10和11之间使用SQL不等于运算符(<>)。

SQL不等于运算符<>和!=之间的区别 (Difference between SQL Not Equal Operator <> and !=)

We can use both SQL Not Equal operators <> and != to do inequality test between two expressions. Both operators give the same output. The only difference is that ‘<>’ is in line with the ISO standard while ‘!=’ does not follow ISO standard. You should use <> operator as it follows the ISO standard.

我们可以同时使用SQL不等于运算符<>和!=在两个表达式之间进行不相等测试。 两个运算符给出相同的输出。 唯一的区别是'<>'符合ISO标准,而'!='不遵循ISO标准。 您应该使用<>运算符,因为它遵循ISO标准。

Let’s set up a sample table to explore SQL Not Equal operator.

让我们建立一个样本表来研究SQL不等于运算符。

CREATE TABLE dbo.Products
(ProductID         INTPRIMARY KEY IDENTITY(1, 1), ProductName       VARCHAR(50), ProductLaunchDate DATETIME2
);

To generate the test data, I used ApexSQL Generate as shown in the following screenshot.

为了生成测试数据,我使用了ApexSQL Generate ,如以下屏幕截图所示。

We can see sample data in the Products table.

我们可以在“产品”表中看到示例数据。

示例1:获取除ProductID 1之外的所有产品详细信息 (Example 1: Get all product details except ProductID 1)

We are going to use SQL Not Equal operator <> to exclude ProductID 1 in the output.

我们将使用SQL不等于运算符<>在输出中排除ProductID 1。

Select * from dbo.products where ProductID <> 1

As stated earlier, we can use != operator as well to get the same output.

如前所述,我们也可以使用!=运算符来获得相同的输出。

Select * from dbo.products where ProductID!=1

示例2:获取2019年推出的产品以外的所有产品的列表 (Example 2: Get a list of all product except those launched in the Year 2019)

Suppose we want to get a list of products that launched except in the year 2019. We can use the following query using SQL Not Equal operator.

假设我们要获取在2019年以外发布的产品列表。我们可以使用SQL Not Equal运算符使用以下查询。

Select * from dbo.products where Year(ProductLaunchDate) <>2019

In the output, we can see all products except those launched in the Year 2019.

在输出中,我们可以看到除2019年推出的产品以外的所有产品。

示例3:获取除特定产品之外的所有产品的列表 (Example 3: Get a list of all products excluding a specific product)

In previous examples, we used SQL Not Operator and specified a numerical value in the WHERE condition. Suppose we want to exclude a particular product from the output. We need to use string or varchar data type with a single quote in the where clause.

在前面的示例中,我们使用SQL Not运算符并在WHERE条件中指定了一个数值。 假设我们要从输出中排除特定产品。 我们需要在where子句中使用带单引号的字符串或varchar数据类型。

Select * from dbo.products where Productname<>'Batchpickphone'

In the output, we do not have productID 10 as it gets excluded from the output.

在输出中,我们没有productID 10,因为它已从输出中排除。

If we do not specify the expression in a single quote, we get the following error message. It treats the expressions as a table column name without the single quote.

如果我们未在单引号中指定表达式,则会收到以下错误消息。 它将表达式视为没有单引号的表列名称。

Msg 207, Level 16, State 1, Line 11 Invalid column name ‘Batchpickphone’.

消息207,级别16,状态1,第11行无效的列名称“ Batchpickphone”。

示例4:使用SQL不等于运算符指定多个条件 (Example 4: Specifying multiple conditions using SQL Not Equal operator)

We can specify multiple conditions in a Where clause to exclude the corresponding rows from an output.

我们可以在Where子句中指定多个条件,以从输出中排除相应的行。

For example, we want to exclude ProductID 1 and ProductName Winitor (having ProductID 2). Execute the following code to satisfy the condition.

例如,我们要排除ProductID 1和ProductName Winitor(具有ProductID 2)。 执行以下代码以满足条件。

Select * from dbo.products where ProductID<>1 and ProductName<>'Winitor''

In the output, we do not have ProductID 1 and ProductID 2.

在输出中,我们没有ProductID 1和ProductID 2。

示例5:SQL不等于运算符和SQL Group By子句 (Example 5: SQL Not Equal operator and SQL Group By clause)

We can use SQL Not Equal operator in combination with the SQL Group By clause. In the following query, we use SQL Group by on ProductLaunchDate column to get a count of products excluding the year 2019.

我们可以将SQL不等于运算符与SQL Group By子句结合使用。 在以下查询中,我们在ProductLaunchDate列上使用SQL Group by来获取不包括2019年的产品数量。

Select Count(ProductLaunchDate)
from dbo.Products
group by ProductLaunchDate
having Year(ProductLaunchDate) <>2019

SQL不等于运算符的性能考虑
(Performance consideration of SQL Not Equal operator
)

In this part, we will explore the performance consideration of SQL Not Equal operator. For this part, let’s keep only 10 records in the products table. It helps to demonstrate the situation quickly.

在这一部分中,我们将探讨SQL不等于运算符的性能注意事项。 对于这一部分,让我们在产品表中仅保留10条记录。 它有助于快速显示情况。

Execute the following query to delete products having ProductID>10.

执行以下查询,删除ProductID> 10的产品。

Delete from products where ProductID>10

We have the following records in the Products table.

我们在产品表中有以下记录。

Let’s execute the following query with the following tasks.

让我们通过以下任务执行以下查询。

  • We use SET STATISTICS IO ON to show statistics of IO activity during query execution 我们使用SET STATISTICS IO ON来显示查询执行期间IO活动的统计信息
  • We use SET STATISTICS TIME to display the time for parse, compile and execute each statement in a query batch 我们使用SET STATISTICS TIME来显示分析,编译和执行查询批处理中的每个语句的时间
  • Enable the Actual Execution plan to show the execution plan used to retrieve results for this query by the query optimizer 启用实际执行计划以显示执行计划,以用于查询优化器检索此查询的结果
Set Statistics IO ON
Set Statistics Time On
Select * from dbo.products where ProductID<>1 and Year(ProductLaunchDate)<>2018 and  ProductName<>'Winitor'

In the message tab, we can see the elapsed time for this query is 52 ms.

在消息选项卡中,我们可以看到此查询的经过时间为52毫秒。

In the actual execution plan of this query, we can see SQL Not Equal predicates along with a Non-clustered index scan operator.

在该查询的实际执行计划中,我们可以看到SQL不等于谓词以及非聚集索引扫描运算符。

Let’s rewrite this query using IN operator. We get the same number of rows in this as well in comparison with a previous query using SQL Not Equal operator.

让我们使用IN运算符重写此查询。 与使用SQL Not Equal运算符的先前查询相比,在此方法中得到的行数也相同。

Set Statistics IO ON
Set Statistics Time On
Select * from dbo.products where ProductID in(5,6,7,8,9)

This time query took less time to return the same number of rows. It took only 1 ms while query with SQL Not Equal took 52 ms.

这次查询花费更少的时间返回相同数量的行。 使用SQL不等于查询仅花费了1毫秒,而花费了52毫秒。

In the Actual Execution plan, it used Clustered Index Seek while SQL Not Equal used

在实际执行计划中,它使用聚簇索引查找,而使用SQL不等于

In the property for the Clustered Index Seek, it uses an equality operator to produce a similar result set.

在“聚簇索引查找”的属性中,它使用相等运算符生成相似的结果集。

  • Caution: We should use the Equality operator to get a better performance in comparison with the SQL Not Equal operator.注意:与SQL Not Equal运算符相比,我们应该使用Equality运算符获得更好的性能。

结论 (Conclusion)

In this article, we explored SQL Not Operator along with examples. We also considered its performance implications in comparison with the Equality operators. You should try to use the Equality operator for better query performance. If you have any comments or questions, feel free to leave them in the comments below.

在本文中,我们与示例一起探讨了SQL Not Operator。 与Equality运算符相比,我们还考虑了其​​性能影响。 您应该尝试使用Equality运算符以获得更好的查询性能。 如果您有任何意见或疑问,请随时将其留在下面的评论中。

翻译自: https://www.sqlshack.com/sql-not-equal-operator/

sql 逻辑运算符不等于

sql 逻辑运算符不等于_SQL不等于运算符介绍和示例相关推荐

  1. C语言关系运算符介绍和示例

    文章目录 1.关系运算符介绍 2.应用示例 3.获取视频教程 4.版权声明 1.关系运算符介绍 关系运算(Relational Operators),用于判断条件,决定程序的流程. 关系 数学中的表示 ...

  2. C语言算术运算符介绍和示例

    文章目录 1.算术运算符 2.获取视频教程 3.版权声明 1.算术运算符 下表显示了 C 语言支持的所有算术运算符.假设变量 A 的值为 18,变量 B 的值为 5,则: 运算符 描述 实例 + 两个 ...

  3. C语言基础入门48篇_13_关系运算符与关系表达式(等于(==)、不等于(叹=)、大于(>)、小于(<)、小于等于(<=)、大于等于(>=),5==nValue方式避免bug,==不可比较浮点型数据)

    C语言中的关系运算符有等于(==).不等于(!=).大于(>).小于(<).小于等于(<=).大于等于(>=).他们可以直接用于整型.浮点基本数据类型及指针类型变量的比较. 1 ...

  4. sql 逻辑运算符_SQL Like逻辑运算符介绍和概述

    sql 逻辑运算符 The SQL Like is a logical operator that is used to determine whether a specific character ...

  5. R语言逻辑运算符(Logical Operators,大于、小于、等于、不等于、与或非、是否为真)、R语言逻辑运算符(Logical Operators)实战示例

    R语言逻辑运算符(Logical Operators,大于.小于.等于.不等于.与或非.是否为真.>.<.!=.==.&.|.!&&.||).R语言逻辑运算符(Lo ...

  6. javascript的等于和不等于运算符用法与功能(三)

    javascript的常见运算符用法与功能(三) 这里所纪录的运算符是javascript中的等于(==)和不等于(!=),这两种运算符. //返回的结果为布尔值. var a='1'; var b= ...

  7. JS比较运算符之等于与不等于详解

    [align=center]JS比较运算符之等于与不等于详解[/align] [b]1.1等于(==)与不等于(!=)[/b] 使用==或!=来比较两个数据是否相等,如果两个数据的类型不同,将进行转换 ...

  8. Sql中的null 与不等于的比较

    sql中的null与不等于符号的比较<>,!=,如果查询的数据中有null的情况,那么 用 is null 或者 is not null, 如果表A中有数据为 A name  code  ...

  9. Oracle和sql语言,SQL语言的四种类型和ORACLE运算符

    数据定义语言(DDL) 数据操作语言(DML) 数据控制语言(DCL) 事务控制语言(TCL) Data Definition Language(DDL) DDL使我们有能力创建或删除表格.也可以定义 ...

最新文章

  1. 什么场景使用mysql的存储过程_mysql存储过程的使用
  2. 多gpu训练梯度如何计算,求和是否要求平均
  3. ReLu(Rectified Linear Units)激活函数
  4. P、NP、NPC(NP完全问题)、NP-hard问题概述
  5. keil debug如何在watch直接修改变量值_python日志记录系列教程,内置logging模块(一),直接使用logging模块的基础日志记录
  6. 这本关于Node.js的书,是一本神书,助你学会Node.js,为你升职加薪,走上人生巅峰
  7. 解决MySQL8小时自动断开连接的问题(DBCP配置)
  8. 【干货】深度学习中的线性代数---简明教程
  9. 一些意想不到的bug
  10. matlab2c使用c++实现matlab函数系列教程-circshift函数
  11. 【PTAL2-001】紧急救援(Dijkstra+最短路径的条数+最短路径中点权之和的最大值)
  12. 搜索引擎登录工具_做seo必须懂的十条搜索引擎语法
  13. 热电偶测温方案 AD7124+Pt100冷端补偿
  14. 微软商店 错误代码0x80073CF9
  15. 推荐关于ElasticSearch的好文
  16. 如何设置快速启动栏 win7 快速启动栏 快速启动栏不见了.
  17. k均值的损失函数_常用的损失函数
  18. 使用C大调编排一首歌曲
  19. 【转】下一代密码模块安全标准探讨
  20. 入行大数据,需要学习哪些编程语言?

热门文章

  1. linux 模拟开放网络,搭建虚假portal网页认证wifi
  2. 【使用最小花费爬楼梯(746-java)】
  3. 通过HTML和CSS设计一个静态网页(练习实例,附完整代码)
  4. 104. 二叉树的最大深度
  5. Java第20天——二叉树的深度遍历的递归实现
  6. 可视计算机应用作业,核心素养背景下可视化教学在中职计算机应用基础中的运用...
  7. 六十星系之18武曲七杀坐卯酉
  8. PAT(乙级)1086 就不告诉你 (15 分)
  9. qt中关于xml的读取、写入、修改等操作
  10. Android获取当前系统时间(12/24小时制)