MySQL(2)

1. 算术运算符:+ - * / div % mod

SELECT 100, 100 + 0, 100 - 0, 100 + 50, 100 + 50 * 30, 100 + 35.5, 100 - 35.5 FROM DUAL;# 在SQL中,+没有连接的作用,就表示加法运算。此时,会将字符串转换为数值(隐式转换)SELECT 100 + '1'  # 在Java语言中,结果是:1001。FROM DUAL;SELECT 100 + 'a' #此时将'a'看做0处理FROM DUAL;SELECT 100 + NULL  # null值参与运算,结果为nullFROM DUAL;

SELECT 100, 100 * 1, 100 * 1.0, 100 / 1.0, 100 / 2,100 + 2 * 5 / 2,100 / 3, 100 DIV 0  # 分母如果为0,则结果为nullFROM DUAL;

# 取模运算:% modSELECT 12 % 3,12 % 5, 12 MOD -5,-12 % 5,-12 % -5FROM DUAL;

#练习:查询员工id为偶数的员工信息SELECT employee_id,last_name,salaryFROM employeesWHERE employee_id % 2 = 0;

2. 比较运算符

#2.1 =  <=>  <> !=  <  <=  >  >= # = 的使用SELECT 1 = 2,1 != 2,1 = '1',1 = 'a',0 = 'a' #字符串存在隐式转换。如果转换数值不成功,则看做0FROM DUAL;
SELECT 'a' = 'a','ab' = 'ab','a' = 'b' #两边都是字符串的话,则按照ANSI的比较规则进行比较。FROM DUAL;
SELECT 1 = NULL,NULL = NULL # 只要有null参与判断,结果就为nullFROM DUAL;
SELECT last_name,salary,commission_pctFROM employees#where salary = 6000;WHERE commission_pct = NULL;  #此时执行,不会有任何的结果# <=> :安全等于。记忆技巧:为NULL而生。SELECT 1 <=> 2,1 <=> '1',1 <=> 'a',0 <=> 'a'FROM DUAL;SELECT 1 <=> NULL, NULL <=> NULLFROM DUAL;#练习:查询表中commission_pct为null的数据有哪些SELECT last_name,salary,commission_pctFROM employeesWHERE commission_pct <=> NULL;
SELECT 3 <> 2,'4' <> NULL, '' != NULL,NULL != NULLFROM DUAL;

2.2

① IS NULL \ IS NOT NULL \ ISNULL

#练习:查询表中commission_pct为null的数据有哪些SELECT last_name,salary,commission_pctFROM employeesWHERE commission_pct IS NULL;#或SELECT last_name,salary,commission_pctFROM employeesWHERE ISNULL(commission_pct);#练习:查询表中commission_pct不为null的数据有哪些SELECT last_name,salary,commission_pctFROM employeesWHERE commission_pct IS NOT NULL;#或SELECT last_name,salary,commission_pctFROM employeesWHERE NOT commission_pct <=> NULL;

② LEAST() \ GREATEST

SELECT LEAST('g','b','t','m'),GREATEST('g','b','t','m')FROM DUAL;SELECT LEAST(first_name,last_name),LEAST(LENGTH(first_name),LENGTH(last_name))FROM employees;

③ BETWEEN 条件下界1 AND 条件上界2 (查询条件1和条件2范围内的数据,包含边界)​​​​​​​

#查询工资在6000 到 8000的员工信息SELECT employee_id,last_name,salaryFROM employees#where salary between 6000 and 8000;WHERE salary >= 6000 && salary <= 8000;#交换6000 和 8000之后,查询不到数据SELECT employee_id,last_name,salaryFROM employeesWHERE salary BETWEEN 8000 AND 6000;#查询工资不在6000 到 8000的员工信息SELECT employee_id,last_name,salaryFROM employeesWHERE salary NOT BETWEEN 6000 AND 8000;#where salary < 6000 or salary > 8000;

④ in (set)\ not in (set)​​​​​​​

#练习:查询部门为10,20,30部门的员工信息SELECT last_name,salary,department_idFROM employees#where department_id = 10 or department_id = 20 or department_id = 30;WHERE department_id IN (10,20,30);#练习:查询工资不是6000,7000,8000的员工信息SELECT last_name,salary,department_idFROM employeesWHERE salary NOT IN (6000,7000,8000);

⑤ LIKE :模糊查询​​​​​​​

# % : 代表不确定个数的字符 (0个,1个,或多个)#练习:查询last_name中包含字符'a'的员工信息SELECT last_nameFROM employeesWHERE last_name LIKE '%a%';#练习:查询last_name中以字符'a'开头的员工信息SELECT last_nameFROM employeesWHERE last_name LIKE 'a%';#练习:查询last_name中包含字符'a'且包含字符'e'的员工信息#写法1:SELECT last_nameFROM employeesWHERE last_name LIKE '%a%' AND last_name LIKE '%e%';#写法2:SELECT last_nameFROM employeesWHERE last_name LIKE '%a%e%' OR last_name LIKE '%e%a%';# _ :代表一个不确定的字符#练习:查询第3个字符是'a'的员工信息SELECT last_nameFROM employeesWHERE last_name LIKE '__a%';#练习:查询第2个字符是_且第3个字符是'a'的员工信息#需要使用转义字符: \ SELECT last_nameFROM employeesWHERE last_name LIKE '_\_a%';#或者  (了解)SELECT last_nameFROM employeesWHERE last_name LIKE '_$_a%' ESCAPE '$';

⑥ REGEXP \ RLIKE :正则表达式​​​​​​​

SELECT 'shkstart' REGEXP '^shk', 'shkstart' REGEXP 't$', 'shkstart' REGEXP 'hk'FROM DUAL;SELECT 'atguigu' REGEXP 'gu.gu','atguigu' REGEXP '[ab]'FROM DUAL;
  1. 逻辑运算符:OR || AND && NOT ! XOR

​​​​​​​

# or  and SELECT last_name,salary,department_idFROM employees#where department_id = 10 or department_id = 20;#where department_id = 10 and department_id = 20;WHERE department_id = 50 AND salary > 6000;# not SELECT last_name,salary,department_idFROM employees#where salary not between 6000 and 8000;#where commission_pct is not null;WHERE NOT commission_pct <=> NULL;# XOR :追求的"异"SELECT last_name,salary,department_idFROM employeesWHERE department_id = 50 XOR salary > 6000;#注意:AND的优先级高于OR
  1. 位运算符: & | ^ ~ >> <<

    SELECT 12 & 5, 12 | 5,12 ^ 5 FROM DUAL;SELECT 10 & ~1 FROM DUAL;#在一定范围内满足:每向左移动1位,相当于乘以2;每向右移动一位,相当于除以2。SELECT 4 << 1 , 8 >> 1FROM DUAL;

课后练习

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

SELECT last_name,salaryFROM employeeswhere salary not between 5000 and 12000;#WHERE salary < 5000 OR salary > 12000;

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

SELECT last_name,department_idFROM employees# where department_id in (20,50);WHERE department_id = 20 OR department_id = 50;

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

SELECT last_name,job_id,manager_idFROM employeesWHERE manager_id IS NULL;SELECT last_name,job_id,manager_idFROM employeesWHERE manager_id <=> NULL;

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

SELECT last_name,salary,commission_pctFROM employeesWHERE commission_pct IS NOT NULL;SELECT last_name,salary,commission_pctFROM employeesWHERE NOT commission_pct <=> NULL;

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

SELECT last_nameFROM employeesWHERE last_name LIKE '__a%';

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

SELECT last_nameFROM employeesWHERE last_name LIKE '%a%k%' OR last_name LIKE '%k%a%';#where last_name like '%a%' and last_name LIKE '%k%';

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

SELECT first_name,last_nameFROM employeesWHERE first_name LIKE '%e';SELECT first_name,last_nameFROM employeesWHERE first_name REGEXP 'e$'; # 以e开头的写法:'^e'

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

SELECT last_name,job_idFROM employees#方式1:推荐WHERE department_id BETWEEN 80 AND 100;#方式2:推荐,与方式1相同#where department_id >= 80 and department_id <= 100;#方式3:仅适用于本题的方式。#where department_id in (80,90,100);SELECT * FROM departments;

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

SELECT last_name,salary,manager_idFROM employeesWHERE manager_id IN (100,101,110);
相关推荐
hai405871 分钟前
Spring Boot整合Redis Stack构建本地向量数据库相似性查询
数据库·spring boot·redis
hummhumm38 分钟前
第33章 - Go语言 云原生开发
java·开发语言·后端·python·sql·云原生·golang
代码小鑫1 小时前
A046-基于SpringBoot的论坛系统的设计与实现
java·开发语言·数据库·spring boot·毕业设计
King.6241 小时前
sql工具!好用!爱用!
大数据·数据库·人工智能·sql·学习
寻找沙漠的人1 小时前
MySQL 索引·事务
数据库·mysql
wenyue11212 小时前
云原生开发框架
数据库·云原生
TPCloud2 小时前
AIX下crs-5005 ip address is aready in use in the network的解决办法
数据库·oracle·crs-5005
安迁岚2 小时前
【SQL Server】华中农业大学空间数据库实验报告 实验八 存储过程
数据库·sql·mysql·oracle
Achou.Wang2 小时前
Redis过期时间和SORT命令的高级用法
数据库·redis·bootstrap
.ccc.。2 小时前
mysql索引失效的五种情况
数据库