举报投诉联系我们 手机版 热门标签 编程学
您的位置:编程学 > 操作符重载 操作符

操作符重载 操作符

2023-04-19 10:18 SQL教程

操作符重载 操作符

操作符重载 操作符

操作符重载

SQL 操作符

每个操作符都是一个保留字,主要用于在 SQL 语句的 WHERE 子句中执行各种操作,例如比较和算术运算等。

操作符在 SQL 语句中指定了条件,并可以将同一语句中的不同条件连接起来。

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

SQL 算术运算符

这里一些有关 SQL 算术运算符如何使用的简单示例:

    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 得 30
- 相减:从最边的操作数中减去右边的操作数。 a - b 得 -10
* 相乘:将两边的操作数相乘。 a * b 得 200
/ 相除:用右边的操作数除以左边的操作数。 b / a 得 2
% 取余:用右边的操作数除以左边的操作数,并返回余数。 b % a 得 0

SQL 比较运算符

考虑 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)

假设变量 a 的值为 10, 变量 b 的值为 20,那么:

操作符 描述 示例
= 检查两个操作数的值是否相等,是的话返回 true。 (a = b) 不为 true。
!= 检查两个操作数的值是否相等,如果不等则返回 true。 (a != b) 为 true。
检查两个操作数的值是否相等,如果不等则返回 true。 (a b) 为真。
> 检查左边的操作数是否大于右边的操作数,是的话返回真。 (a > b) 不为 true。
检查左边的操作数是否小于右边的操作数,是的话返回真。 (a
>= 检查左边的操作数是否大于或等于右边的操作数,是的话返回真。 (a >= b) 不为 true。
检查左边的操作数是否小于或等于右边的操作数,是的话返回真。 (a
! 检查左边的操作数是否不小于右边的操作数,是的话返回真。 (a !
!> 检查左边的操作数是否不大于右边的操作数,是的话返回真。 (a !> b) 为 true。

SQL 逻辑运算符

考虑 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 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)

下面列出了 SQL 中可用的逻辑运算符。

运算符 描述
ALL ALL 运算符用于将一个值同另一个值集中所有的值进行比较。
AND AND 运算符使得在 WHERE 子句中可以同时存在多个条件。
ANY ANY 运算符用于将一个值同条件所指定的列表中的任意值相比较。
BETWEEN 给定最小值和最大值,BETWEEN 运算符可以用于搜索区间内的值。
EXISTS EXISTS 运算符用于在表中搜索符合特定条件的行。
IN IN 运算符用于将某个值同指定的一列字面值相比较。
LIKE LIKE 运算符用于使用通配符对某个值和与其相似的值做出比较。
NOT NOT 操作符反转它所作用的操作符的意义。例如,NOT EXISTS、NOT BETWEEN、NOT IN 等。这是一个求反运算符。
OR OR 运算符用于在 SQL 语句中连接多个条件。
IS NULL NULL Operator 用于将某个值同 NULL 作比较。
UNIQUE UNIQUE 运算符检查指定表的所有行,以确定没有重复。
阅读全文
以上是编程学为你收集整理的操作符重载 操作符全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 编程学 bianchengxue.com 版权所有 联系我们
桂ICP备19012293号-7 返回底部