扫码一下
查看教程更方便
运算符是一些保留的关键词或特殊的字符,主要用于 sql 语句的 where 子句中,从而执行特定的操作,例如比较和算术运算。这些运算符用于指定 sql 语句中的条件,并用作语句中多个条件的连接词。
假设“变量 a”为10,“变量 b”为20
运算符 | 描述 | 例子 |
---|---|---|
(加法) | 在运算符的任一侧添加值。 | a b = 30 |
- (减法) | 从左边操作数中减去右边操作数。 | a - b = -10 |
* (乘法) | 将运算符两侧的值相乘。 | a * b = 200 |
/ (除法) | 将左边操作数除以右边操作数。 | b / a = 2 |
% (模除) | 将左边操作数除以右边操作数并返回余数。 | b % a = 0 |
sql> select 10 20;
结果
--------
| 10 20 |
--------
| 30 |
--------
1 row in set (0.00 sec)
sql> select 10 * 20;
结果
---------
| 10 * 20 |
---------
| 200 |
---------
1 row in set (0.00 sec)
sql> select 10 / 5;
结果
--------
| 10 / 5 |
--------
| 2.0000 |
--------
1 row in set (0.03 sec)
sql> select 12 % 5;
结果
---------
| 12 % 5 |
---------
| 2 |
---------
1 row in set (0.00 sec)
假设“变量 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 比较运算符的用法
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)
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)
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)
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)
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 中可用的所有逻辑运算符的列表。
运算符 | 描述 |
---|---|
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 表
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)
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)
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)
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)
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)
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)
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)
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)
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)