教程 > SQL 教程 > SQL 基础 阅读:39

SQL 运算符

SQL 中的运算符是什么?

运算符是一些保留的关键词或特殊的字符,主要用于 SQL 语句的 WHERE 子句中,从而执行特定的操作,例如比较和算术运算。这些运算符用于指定 SQL 语句中的条件,并用作语句中多个条件的连接词。

  • 算术运算符
  • 比较运算符
  • 逻辑运算符
  • 用于否定条件的运算符

SQL 算术运算符

假设“变量 a”为10,“变量 b”为20

运算符 描述 例子
+ (加法) 在运算符的任一侧添加值。 a + b = 30
- (减法) 从左边操作数中减去右边操作数。 a - b = -10
* (乘法) 将运算符两侧的值相乘。 a * b = 200
/ (除法) 将左边操作数除以右边操作数。 b / a = 2
% (模除) 将左边操作数除以右边操作数并返回余数。 b % a = 0

示例

示例 1

SQL> select 10+ 20;

结果

+--------+
| 10+ 20 |
+--------+
|     30 |
+--------+
1 row in set (0.00 sec)

示例 2

SQL> select 10 * 20;

结果

+---------+
| 10 * 20 |
+---------+
|     200 |
+---------+
1 row in set (0.00 sec)

示例 3

SQL> select 10 / 5;

结果

+--------+
| 10 / 5 |
+--------+
| 2.0000 |
+--------+
1 row in set (0.03 sec)

示例 4

SQL> select 12 %  5;

结果

+---------+
| 12 %  5 |
+---------+
|       2 |
+---------+
1 row in set (0.00 sec)

SQL 比较运算符

假设“变量 a”为10,“变量 b”为20

运算符 描述 例子
= 检查两个操作数的值是否相等,如果是,则条件为真。 (a = b) 为假。
!= 检查两个操作数的值是否相等,如果值不相等则条件为真。 (a != b) 为真。
<> 检查两个操作数的值是否相等,如果值不相等则条件为真。 (a <> b) 为真。
> 检查左操作数的值是否大于右操作数的值,如果是,则条件为真。 (a > b) 为假。
< 检查左操作数的值是否小于右操作数的值,如果是,则条件为真。 (a < b) 为真。
>= 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件为真。 (a >= b) 为假。
<= 检查左操作数的值是否小于或等于右操作数的值,如果是则条件成立。 (a <= b) 为真。
!< 检查左操作数的值是否不小于右操作数的值,如果是则条件成立。 (a !< b) 为假。
!> 检查左操作数的值是否不大于右操作数的值,如果是,则条件为真。 (a !> b) 为真。

示例

具有以下记录的 CUSTOMERS 表

SQL> SELECT * FROM CUSTOMERS;
+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+
7 rows in set (0.00 sec)

以下是一些简单的例子,展示了 SQL 比较运算符的用法

示例 1

SQL> SELECT * FROM CUSTOMERS WHERE SALARY > 5000;

结果

+----+----------+-----+---------+----------+
| ID | NAME     | AGE | ADDRESS | SALARY   |
+----+----------+-----+---------+----------+
|  4 | Chaitali |  25 | Mumbai  |  6500.00 |
|  5 | Hardik   |  27 | Bhopal  |  8500.00 |
|  7 | Muffy    |  24 | Indore  | 10000.00 |
+----+----------+-----+---------+----------+
3 rows in set (0.00 sec)

示例 2

SQL>  SELECT * FROM CUSTOMERS WHERE SALARY = 2000;

结果

+----+---------+-----+-----------+---------+
| ID | NAME    | AGE | ADDRESS   | SALARY  |
+----+---------+-----+-----------+---------+
|  1 | Ramesh  |  32 | Ahmedabad | 2000.00 |
|  3 | kaushik |  23 | Kota      | 2000.00 |
+----+---------+-----+-----------+---------+
2 rows in set (0.00 sec)

示例 3

SQL>  SELECT * FROM CUSTOMERS WHERE SALARY != 2000;

结果

+----+----------+-----+---------+----------+
| ID | NAME     | AGE | ADDRESS | SALARY   |
+----+----------+-----+---------+----------+
|  2 | Khilan   |  25 | Delhi   |  1500.00 |
|  4 | Chaitali |  25 | Mumbai  |  6500.00 |
|  5 | Hardik   |  27 | Bhopal  |  8500.00 |
|  6 | Komal    |  22 | MP      |  4500.00 |
|  7 | Muffy    |  24 | Indore  | 10000.00 |
+----+----------+-----+---------+----------+
5 rows in set (0.00 sec)

示例 4

SQL> SELECT * FROM CUSTOMERS WHERE SALARY <> 2000;

结果

+----+----------+-----+---------+----------+
| ID | NAME     | AGE | ADDRESS | SALARY   |
+----+----------+-----+---------+----------+
|  2 | Khilan   |  25 | Delhi   |  1500.00 |
|  4 | Chaitali |  25 | Mumbai  |  6500.00 |
|  5 | Hardik   |  27 | Bhopal  |  8500.00 |
|  6 | Komal    |  22 | MP      |  4500.00 |
|  7 | Muffy    |  24 | Indore  | 10000.00 |
+----+----------+-----+---------+----------+
5 rows in set (0.00 sec)

示例 5

SQL> SELECT * FROM CUSTOMERS WHERE SALARY >= 6500;

结果

+----+----------+-----+---------+----------+
| ID | NAME     | AGE | ADDRESS | SALARY   |
+----+----------+-----+---------+----------+
|  4 | Chaitali |  25 | Mumbai  |  6500.00 |
|  5 | Hardik   |  27 | Bhopal  |  8500.00 |
|  7 | Muffy    |  24 | Indore  | 10000.00 |
+----+----------+-----+---------+----------+
3 rows in set (0.00 sec)

SQL 逻辑运算符

下面是 SQL 中可用的所有逻辑运算符的列表。

运算符 描述
ALL ALL 运算符用于将一个值与另一个值的集合中的所有值进行比较。
AND AND 运算符允许在 SQL 语句的 WHERE 子句中存在多个条件。
ANY ANY 运算符用于根据条件将值与列表中的任何适用值进行比较。
BETWEEN BETWEEN 运算符用于在给定最小值和最大值的情况下搜索一组值中的值。
EXISTS EXISTS 运算符用于搜索指定表中是否存在满足特定条件的行。
IN IN 运算符用于将值与已指定的文字值列表进行比较。
LIKE LIKE 运算符用于使用通配符运算符将值与相似值进行比较。
NOT NOT 运算符颠倒了与它一起使用的逻辑运算符的含义。例如:NOT EXISTS、NOT BETWEEN、NOT IN 等。这是一个否定运算符。
OR OR 运算符用于在 SQL 语句的 WHERE 子句中组合多个条件。
IS NULL NULL 运算符用于将值与 NULL 值进行比较。
UNIQUE UNIQUE 运算符搜索指定表的每一行的唯一性(无重复)。

示例

还是使用上面的 CUSTOMERS 表

示例 1

SQL> SELECT * FROM CUSTOMERS WHERE AGE >= 25 AND SALARY >= 6500;

结果

+----+----------+-----+---------+---------+
| ID | NAME     | AGE | ADDRESS | SALARY  |
+----+----------+-----+---------+---------+
|  4 | Chaitali |  25 | Mumbai  | 6500.00 |
|  5 | Hardik   |  27 | Bhopal  | 8500.00 |
+----+----------+-----+---------+---------+
2 rows in set (0.00 sec)

示例 2

SQL> SELECT * FROM CUSTOMERS WHERE AGE >= 25 OR SALARY >= 6500;

结果

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+
5 rows in set (0.00 sec)

示例 3

SQL>  SELECT * FROM CUSTOMERS WHERE AGE IS NOT NULL;

结果

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+
7 rows in set (0.00 sec)

示例 4

SQL> SELECT * FROM CUSTOMERS WHERE NAME LIKE 'Ko%';

结果

+----+-------+-----+---------+---------+
| ID | NAME  | AGE | ADDRESS | SALARY  |
+----+-------+-----+---------+---------+
|  6 | Komal |  22 | MP      | 4500.00 |
+----+-------+-----+---------+---------+
1 row in set (0.00 sec)

示例 5

SQL> SELECT * FROM CUSTOMERS WHERE AGE IN ( 25, 27 );

结果

+----+----------+-----+---------+---------+
| ID | NAME     | AGE | ADDRESS | SALARY  |
+----+----------+-----+---------+---------+
|  2 | Khilan   |  25 | Delhi   | 1500.00 |
|  4 | Chaitali |  25 | Mumbai  | 6500.00 |
|  5 | Hardik   |  27 | Bhopal  | 8500.00 |
+----+----------+-----+---------+---------+
3 rows in set (0.00 sec)

示例 6

SQL> SELECT * FROM CUSTOMERS WHERE AGE BETWEEN 25 AND 27;

结果

+----+----------+-----+---------+---------+
| ID | NAME     | AGE | ADDRESS | SALARY  |
+----+----------+-----+---------+---------+
|  2 | Khilan   |  25 | Delhi   | 1500.00 |
|  4 | Chaitali |  25 | Mumbai  | 6500.00 |
|  5 | Hardik   |  27 | Bhopal  | 8500.00 |
+----+----------+-----+---------+---------+
3 rows in set (0.00 sec)

示例 7

SQL> SELECT AGE FROM CUSTOMERS WHERE EXISTS (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);

结果

+-----+
| AGE |
+-----+
|  32 |
|  25 |
|  23 |
|  25 |
|  27 |
|  22 |
|  24 |
+-----+
7 rows in set (0.02 sec)

示例 8

SQL> SELECT * FROM CUSTOMERS WHERE AGE > ALL (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);

结果

+----+--------+-----+-----------+---------+
| ID | NAME   | AGE | ADDRESS   | SALARY  |
+----+--------+-----+-----------+---------+
|  1 | Ramesh |  32 | Ahmedabad | 2000.00 |
+----+--------+-----+-----------+---------+
1 row in set (0.02 sec)

示例 9

SQL> SELECT * FROM CUSTOMERS WHERE AGE > ANY (SELECT AGE FROM CUSTOMERS WHERE SALARY > 6500);

结果

+----+----------+-----+-----------+---------+
| ID | NAME     | AGE | ADDRESS   | SALARY  |
+----+----------+-----+-----------+---------+
|  1 | Ramesh   |  32 | Ahmedabad | 2000.00 |
|  2 | Khilan   |  25 | Delhi     | 1500.00 |
|  4 | Chaitali |  25 | Mumbai    | 6500.00 |
|  5 | Hardik   |  27 | Bhopal    | 8500.00 |
+----+----------+-----+-----------+---------+
4 rows in set (0.00 sec)

查看笔记

扫码一下
查看教程更方便