MySQL---运算符

运算符

  • 算术运算符
    • 加减乘除运算符
    • 求模(取余)运算符
  • 比较运算符
    • 等号运算符(=)
    • 安全等于运算符(<=>)
    • 不等于运算符(<>或!=)
    • 空运算符(IS NULL或ISNULL)
    • 非空运算符(IS NOT NULL)
    • 最小值运算符(least(值1,值2,,,值n))
    • 最大值运算符(greatest(值1,值2,,,值n))
    • between and运算符
    • in运算符
    • not in运算符
    • like运算符
    • regexp运算符
  • 逻辑运算符
    • 逻辑非运算符(not或!)
    • 逻辑与运算符(and或&&)
    • 逻辑或运算符(or或||)
    • 逻辑异或运算符(xor)
  • 位运算符
    • 按位与运算符(&)
    • 按位或运算符(|)
    • 按位异或(^)
    • 按位取反运算符(~)
    • 按位右移运算符(>>)
    • 按位左移运算符(<<)
  • 运算符的优先级
  • 运算符练习
    • 1.选择工资不在5000到12000的员工的姓名和工资
    • 2.选择在20或50号部门工作的员工姓名和部门号
    • 3.选择公司中没有管理者的员工姓名及job_id
    • 4.选择公司中有奖金的员工姓名,工资和奖金级别
    • 5.选员工姓名的第三个字母是a的员工姓名
    • 6.选择姓名中有字母a和k的员工姓名
    • 7.显示出表 employees 表中 first_name 以 'e'结尾的员工信息
    • 8.显示出表 employees 部门编号在 80-100 之间的姓名、工种
    • 9.显示出表 employees 的 manager_id 是 100,101,110 的员工姓名、工资、管理者id

算术运算符

加减乘除运算符

SELECT 100,100+1,100-1,100*2,100 DIV 3
FROM DUAL;

注意:
一个整数类型的值对整数进行加法和减法操作,结果还是一个整数;
一个整数类型的值对浮点数进行加法和减法操作,结果是一个浮点数;
加法和减法的优先级相同,进行先加后减操作与进行先减后加操作的结果是一样的;
在Java中,+的左右两边如果有字符串,那么表示字符串的拼接。但是在MySQL中+只表示数
值相加。如果遇到非数值类型,先尝试转成数值,如果转失败,就按0计算。(补充:MySQL
中字符串拼接要使用字符串函数CONCAT()实现)

一个数乘以整数1和除以整数1后仍得原数;
一个数乘以浮点数1和除以浮点数1后变成浮点数,数值与原数相等;
一个数除以整数后,不管是否能除尽,结果都为一个浮点数;
一个数除以另一个数,除不尽时,结果为一个浮点数,并保留到小数点后4位;
乘法和除法的优先级相同,进行先乘后除操作与先除后乘操作,得出的结果相同。
在数学运算中,0不能用作除数,在MySQL中,一个数除以0NULL

求模(取余)运算符

select 12 % 3,12 mod 5
from dual;
筛选出employee_id是偶数的员工
SELECT * 
FROM employees
WHERE employee_id MOD 2 = 0;

比较运算符

等号运算符(=)

mysql> SELECT 1 = 1, 1 = '1', 1 = 0, 'a' = 'a', (5 + 3) = (2 + 6), '' = NULL , NULL =
NULL;
+-------+---------+-------+-----------+-------------------+-----------+-------------+
| 1 = 1 | 1 = '1' | 1 = 0 | 'a' = 'a' | (5 + 3) = (2 + 6) | '' = NULL | NULL = NULL |
+-------+---------+-------+-----------+-------------------+-----------+-------------+
|   1   |    1    |   0   |     1     |         1         |    NULL   |     NULL    |
+-------+---------+-------+-----------+-------------------+-----------+-------------+
mysql> SELECT 1 = 2, 0 = 'abc', 1 = 'abc' FROM dual;
+-------+-----------+-----------+
| 1 = 2 | 0 = 'abc' | 1 = 'abc' |
+-------+-----------+-----------+
|   0   |     1     |    0      |
+-------+-----------+-----------+

安全等于运算符(<=>)

mysql> SELECT 1 <=> '1', 1 <=> 0, 'a' <=> 'a', (5 + 3) <=> (2 + 6), '' <=> NULL,NULL
	   <=> NULL 
       FROM dual;
+-----------+---------+-------------+---------------------+-------------+---------+
| 1 <=> '1' | 1 <=> 0 | 'a' <=> 'a' | (5 + 3) <=> (2 + 6) | '' <=> NULL | NULL <=>NULL |
+-----------+---------+-------------+---------------------+-------------+--------------+
|    1      |    0    |      1      |          1          |     0       |       1      |
+-----------+---------+-------------+---------------------+-------------+--------------+

不等于运算符(<>或!=)

mysql> SELECT 1 <> 1, 1 != 2, 'a' != 'b', (3+4) <> (2+6), 'a' != NULL, NULL <> NULL;
+--------+--------+------------+----------------+-------------+--------------+
| 1 <> 1 | 1 != 2 | 'a' != 'b' | (3+4) <> (2+6) | 'a' != NULL | NULL <> NULL |
+--------+--------+------------+----------------+-------------+--------------+
|    0   |   1    |      1     |        1       |     NULL    |      NULL    |
+--------+--------+------------+----------------+-------------+--------------+

空运算符(IS NULL或ISNULL)

判断一个值是否为NULL,如果为NULL则返回1,否则返回0

mysql> SELECT NULL IS NULL, ISNULL(NULL), ISNULL('a'), 1 IS NULL;
+--------------+--------------+-------------+-----------+
| NULL IS NULL | ISNULL(NULL) | ISNULL('a') | 1 IS NULL |
+--------------+--------------+-------------+-----------+
|      1       |      1       |      0      |     0     |
+--------------+--------------+-------------+-----------+
#查询commission_pct等于NULL。比较如下的四种写法
SELECT employee_id,commission_pct FROM employees WHERE commission_pct IS NULL;
SELECT employee_id,commission_pct FROM employees WHERE commission_pct <=> NULL;
SELECT employee_id,commission_pct FROM employees WHERE ISNULL(commission_pct);
SELECT employee_id,commission_pct FROM employees WHERE commission_pct = NULL;
SELECT last_name, manager_id
FROM employees
WHERE manager_id IS NULL;

非空运算符(IS NOT NULL)

判断一个值是否不为NULL,如果不为NULL则返回1,否则返回0

mysql> SELECT NULL IS NOT NULL, 'a' IS NOT NULL, 1 IS NOT NULL;
+------------------+-----------------+---------------+
| NULL IS NOT NULL | 'a' IS NOT NULL | 1 IS NOT NULL |
+------------------+-----------------+---------------+
|         0        |        1        |       1       |
+------------------+-----------------+---------------+

最小值运算符(least(值1,值2,,,值n))

mysql> SELECT LEAST (1,0,2), LEAST('b','a','c'), LEAST(1,NULL,2);
+---------------+--------------------+-----------------+
| LEAST (1,0,2) | LEAST('b','a','c') | LEAST(1,NULL,2) |
+---------------+--------------------+-----------------+
|        0      |          a         |       NULL      |
+---------------+--------------------+-----------------+

最大值运算符(greatest(值1,值2,,,值n))

mysql> SELECT GREATEST(1,0,2), GREATEST('b','a','c'), GREATEST(1,NULL,2);
+-----------------+-----------------------+--------------------+
| GREATEST(1,0,2) | GREATEST('b','a','c') | GREATEST(1,NULL,2) |
+-----------------+-----------------------+--------------------+
|        2        |           c           |        NULL        |
+-----------------+-----------------------+--------------------+

between and运算符

mysql> SELECT 1 BETWEEN 0 AND 1, 10 BETWEEN 11 AND 12, 'b' BETWEEN 'a' AND 'c';
+-------------------+----------------------+-------------------------+
| 1 BETWEEN 0 AND 1 | 10 BETWEEN 11 AND 12 | 'b' BETWEEN 'a' AND 'c' |
+-------------------+----------------------+-------------------------+
|         1         |            0         |             1           |
+-------------------+----------------------+-------------------------+
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500;
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500;

in运算符

IN运算符用于判断给定的值是否是IN列表中的一个值,如果是则返回1,否则返回0。如果给
定的值为NULL,或者IN列表中存在NULL,则结果为NULL

mysql> SELECT 'a' IN ('a','b','c'), 1 IN (2,3), NULL IN ('a','b'), 'a' IN ('a', NULL);
+----------------------+------------+-------------------+--------------------+
| 'a' IN ('a','b','c') | 1 IN (2,3) | NULL IN ('a','b') | 'a' IN ('a', NULL) |
+----------------------+------------+-------------------+--------------------+
|          1           |      0     |        NULL       |          1         |
+----------------------+------------+-------------------+--------------------+

not in运算符

NOT IN运算符用于判断给定的值是否不是IN列表中的一个值,如果不是IN列表中的一
个值,则返回1,否则返回0

mysql> SELECT 'a' NOT IN ('a','b','c'), 1 NOT IN (2,3);
+--------------------------+----------------+
| 'a' NOT IN ('a','b','c') | 1 NOT IN (2,3) |
+--------------------------+----------------+
|              0           |        1       |
+--------------------------+----------------+

like运算符

LIKE运算符主要用来匹配字符串,通常用于模糊匹配,如果满足条件则返回1,否则返回
0。如果给定的值或者匹配条件为NULL,则返回结果为NULL

LIKE运算符通常使用如下通配符:
“%”:匹配0个或多个字符。
“_”:只能匹配一个字符。

查询last_name中包含字符'a'的员工信息
SELECT last_name
FROM employees
WHERE last_name LIKE '%a%';

查询last_name中以字符'a'开头的员工信息
SELECT last_name
FROM employees
WHERE last_name LIKE 'a%';

查询last_name中包含字符'a'且包含字符'e'的员工信息
写法1SELECT last_name
FROM employees
WHERE last_name LIKE '%a%' AND last_name LIKE '%e%';
写法2SELECT last_name
FROM employees
WHERE last_name LIKE '%a%e%' OR last_name LIKE '%e%a%';

查询第3个字符是'a'的员工信息
SELECT last_name
FROM employees
WHERE last_name LIKE '__a%';

查询第2个字符是_且第3个字符是'a'的员工信息
需要使用转义字符: \ 
SELECT last_name
FROM employees
WHERE last_name LIKE '_\_a%';
或者  (了解)
SELECT last_name
FROM employees
WHERE last_name LIKE '_$_a%' ESCAPE '$';

regexp运算符

REGEXP运算符用来匹配字符串,语法格式为: expr REGEXP 匹配条件 。如果expr满足匹配条件,返回1;如
果不满足,返回0;若expr或匹配条件任意一个为则结果为null

REGEXP运算符在进行匹配时,常用的有以下通配符:
(1)‘^’匹配以该字符后面的字符开头的字符串。
(2)‘$’匹配以该字符前面的字符结尾的字符串。
(3)‘.’匹配任何一个单字符。
(4)“[...]”匹配在方括号内的任何字符。例如,“[abc]”匹配“a”或“b”或“c”。为了命名字符的范围,使用
一个‘-’。“[a-z]”匹配任何字母,而“[0-9]”匹配任何数字。
(5)‘*’匹配零个或多个在它前面的字符。例如,“x*”匹配任何数量的‘x’字符,“[0-9]*”匹配任何数量的数
字,而“*”匹配任何数量的任何字符

mysql> SELECT 'shkstart' REGEXP '^s', 'shkstart' REGEXP 't$', 'shkstart' REGEXP 'hk';
+------------------------+------------------------+-------------------------+
| 'shkstart' REGEXP '^s' | 'shkstart' REGEXP 't$' | 'shkstart' REGEXP 'hk'  |
+------------------------+------------------------+-------------------------+
|           1            |            1           |              1          |
+------------------------+------------------------+-------------------------+
mysql> SELECT 'atguigu' REGEXP 'gu.gu', 'atguigu' REGEXP '[ab]';
+--------------------------+-------------------------+
| 'atguigu' REGEXP 'gu.gu' | 'atguigu' REGEXP '[ab]' |
+--------------------------+-------------------------+
|             1            |             1           |
+--------------------------+-------------------------+

逻辑运算符

逻辑非运算符(not或!)

mysql> SELECT NOT 1, NOT 0, NOT(1+1), NOT !1, NOT NULL;
+-------+-------+----------+--------+----------+
| NOT 1 | NOT 0 | NOT(1+1) | NOT !1 | NOT NULL |
+-------+-------+----------+--------+----------+
|   0   |   1   |     0    |    1   |    NULL  |
+-------+-------+----------+--------+----------+

逻辑与运算符(and或&&)

mysql> SELECT 1 AND -1, 0 AND 1, 0 AND NULL, 1 AND NULL;
+----------+---------+------------+------------+
| 1 AND -1 | 0 AND 1 | 0 AND NULL | 1 AND NULL |
+----------+---------+------------+------------+
|     1    |    0    |      0     |    NULL    |
+----------+---------+------------+------------+

逻辑或运算符(or或||)

mysql> SELECT 1 OR -1, 1 OR 0, 1 OR NULL, 0 || NULL, NULL || NULL;
+---------+--------+-----------+-----------+--------------+
| 1 OR -1 | 1 OR 0 | 1 OR NULL |  0 || NULL | NULL || NULL |
+---------+--------+-----------+-----------+--------------+
|    1    |    1   |      1    |     NULL   |     NULL   |
+---------+--------+-----------+-----------+--------------+
#查询基本薪资不在9000-12000之间的员工编号和基本薪资
SELECT employee_id,salary FROM employees
WHERE NOT (salary >= 9000 AND salary <= 12000);

SELECT employee_id,salary FROM employees
WHERE salary <9000 OR salary > 12000;

SELECT employee_id,salary FROM employees
WHERE salary NOT BETWEEN 9000 AND 12000;

SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >= 10000
OR job_id LIKE '%MAN%';

OR可以和AND一起使用,但是在使用时要注意两者的优先级,由于AND的优先级高于OR,因此先
对AND两边的操作数进行操作,再与OR中的操作数结合

逻辑异或运算符(xor)

mysql> SELECT 1 XOR -1, 1 XOR 0, 0 XOR 0,1 XOR NULL,1 XOR 1 XOR 1,0 XOR 0 XOR 0
+----------+---------+---------+------------+---------------+---------------+
| 1 XOR -1 | 1 XOR 0 | 0 XOR 0 | 1 XOR NULL | 1 XOR 1 XOR 1 | 0 XOR 0 XOR 0 |
+----------+---------+---------+------------+---------------+---------------+
|     0    |    1    |    0    |    NULL    |       1       |       0       |
+----------+---------+---------+------------+---------------+---------------+
select last_name,department_id,salary
from employees
where department_id in (10,20) XOR salary > 8000;

位运算符

按位与运算符(&)

mysql> SELECT 1 & 10, 20 & 30;
+--------+---------+
| 1 & 10 | 20 & 30 |
+--------+---------+
|   0    |    20   |
+--------+---------+

按位或运算符(|)

mysql> SELECT 1 | 10, 20 | 30;
+--------+---------+
| 1 | 10 | 20 | 30 |
+--------+---------+
|   11   |    30   |
+--------+---------+

按位异或(^)

mysql> SELECT 1 ^ 10, 20 ^ 30;
+--------+---------+
| 1 ^ 10 | 20 ^ 30 |
+--------+---------+
|   11   |    10   |
+--------+---------+

按位取反运算符(~)

mysql> SELECT 10 & ~1;
+---------+
| 10 & ~1 |
+---------+
|    10   |
+---------+

按位右移运算符(>>)

mysql> SELECT 1 >> 2, 4 >> 2;
+--------+--------+
| 1 >> 2 | 4 >> 2 |
+--------+--------+
|    0   |    1   |
+--------+--------+

按位左移运算符(<<)

mysql> SELECT 1 << 2, 4 << 2;
+--------+--------+
| 1 << 2 | 4 << 2 |
+--------+--------+
|    4   |   16   |
+--------+--------+

运算符的优先级

MySQL---运算符_第1张图片
数字编号越大,优先级越高,优先级高的运算符先进行计算。可以看到,赋值运算符的优先级最低,使用“()”括起来的表达式的优先级最高

运算符练习

1.选择工资不在5000到12000的员工的姓名和工资

SELECT last_name,salary
FROM employees
WHERE salary NOT BETWEEN 5000 AND 12000;SELECT last_name,salary
FROM employees
WHERE salary < 5000 OR salary > 12000;

2.选择在20或50号部门工作的员工姓名和部门号

select last_name,department_id
from employees
where department_id in(20,50);SELECT last_name,department_id
FROM employees
where department_id = 10 or department_id = 50;

3.选择公司中没有管理者的员工姓名及job_id

select last_name,job_id
from employees
where manager_id is null;select last_name,job_id
from employees
where isnull(manager_id);

4.选择公司中有奖金的员工姓名,工资和奖金级别

select last_name,salary,commission_pct
from employees
where not isnull(salary);select last_name,salary,commission_pct
from employees
where salary is not null;

5.选员工姓名的第三个字母是a的员工姓名

SELECT last_name
FROM employees
WHERE last_name LIKE '__a%';

6.选择姓名中有字母a和k的员工姓名

SELECT last_name
FROM employees
WHERE last_name LIKE '%a%k%' OR last_name LIKE '%k%a%';

7.显示出表 employees 表中 first_name 以 'e’结尾的员工信息

SELECT employee_id,first_name,last_name
FROM employees
WHERE first_name LIKE '%e';SELECT employee_id,first_name,last_name
FROM employees
WHERE first_name REGEXP 'e$';

8.显示出表 employees 部门编号在 80-100 之间的姓名、工种

SELECT last_name,job_id
FROM employees
WHERE department_id BETWEEN 80 AND 100;

9.显示出表 employees 的 manager_id 是 100,101,110 的员工姓名、工资、管理者id

select last_name,salary,manager_id
from employees
where manager_id in(100,101,110);

你可能感兴趣的:(mysql,数据库)