sql逻辑运算符不等于_SQL不等于运算符介绍和⽰例
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
equals不等于>=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        INT
PRIMARY KEY IDENTITY(1, 1),
ProductName      VARCHAR(50),
ProductLaunchDate DATETIME2
);
To generate the test data, I used as shown in the following screenshot.
为了⽣成测试数据,我使⽤了 ,如以下屏幕截图所⽰。
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.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。