关系运算符

运算符

含义

=

等于

<>

不等于

>

大于

>=

大于或等于

<

小于

<=

小于或等于

LIKE

匹配模式

NOT LIKE

不匹配模式

IS NULL

等于 Null

IS NOT NULL

不等于 Null

BETWEEN

下限和上限之间值的范围

IN

一组指定值的成员或子查询的成员

NOT IN

并非一组指定值的成员或子查询的成员

EXISTS

如果子查询返回至少一个记录,则为“True”

ANY

将某个值与子查询返回的每个值进行比较(运算符前面必须是 =、<>、>、>=、< 或 <=);=Any 相当于 In

ALL

将某个值与子查询返回的每个值进行比较(运算符前面必须是 =、<>、>、>=、< 或 <=)

示例

复制
SELECT Sales_Data.Invoice_ID FROM Sales_Data
   WHERE Sales_Data.Salesperson_ID = 'SP-1'
   
SELECT Sales_Data.Amount FROM Sales_Data WHERE Sales_Data.Invoice_ID <> 125

SELECT Sales_Data.Amount FROM Sales_Data WHERE Sales_Data.Amount > 3000

SELECT Sales_Data.Time_Sold FROM Sales_Data
   WHERE Sales_Data.Time_Sold < '12:00:00'

SELECT Sales_Data.Company_Name FROM Sales_Data
   WHERE Sales_Data.Company_Name LIKE '%University'

SELECT Sales_Data.Company_Name FROM Sales_Data
   WHERE Sales_Data.Company_Name NOT LIKE '%University'

SELECT Sales_Data.Amount FROM Sales_Data WHERE Sales_Data.Amount IS NULL

SELECT Sales_Data.Amount FROM Sales_Data WHERE Sales_Data.Amount IS NOT NULL

SELECT Sales_Data.Invoice_ID FROM Sales_Data
   WHERE Sales_Data.Invoice_ID BETWEEN 1 AND 10

SELECT COUNT(Sales_Data.Invoice_ID) AS agg
   FROM Sales_Data WHERE Sales_Data.INVOICE_ID IN (50,250,100)

SELECT COUNT(Sales_Data.Invoice_ID) AS agg
   FROM Sales_Data WHERE Sales_Data.INVOICE_ID NOT IN (50,250,100)

SELECT COUNT(Sales_Data.Invoice_ID) AS agg FROM Sales_Data 
   WHERE Sales_Data.INVOICE_ID NOT IN (SELECT Sales_Data.Invoice_ID
   FROM Sales_Data WHERE Sales_Data.Salesperson_ID = 'SP-4')

SELECT *
   FROM Sales_Data WHERE EXISTS (SELECT Sales_Data.Amount
   FROM Sales_Data WHERE Sales_Data.Salesperson_ID IS NOT NULL)

SELECT *
   FROM Sales_Data WHERE Sales_Data.Amount = ANY (SELECT Sales_Data.Amount
   FROM Sales_Data WHERE Sales_Data.Salesperson_ID = 'SP-1')

SELECT *
   FROM Sales_Data WHERE Sales_Data.Amount = ALL (SELECT Sales_Data.Amount
   FROM Sales_Data WHERE Sales_Data.Salesperson_ID IS NULL)