mysql常用运算符

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: mysql常用运算符

一、去重和空值

学习运算符之前我们先讨论一下去重和空值的问题:

这是我们要操作的表

select DEPARTMENT_ID from employees order by EMPLOYEE_ID ;

查看部门id,发现很多都是一个重复的

1.distinct

select distinct DEPARTMENT_ID from employees order by DEPARTMENT_ID;

2.null参与运算

select employee_id  , salary "月工资" , salary * ( 1 + commission_pct ) * 12 "年工资" from employees;

运算中有一个null,结果为null

3.用ifnull函数解决问题

select employee_id , salary “月工资” , salary * ( 1 + IFNULL(commission_pct,0) ) * 12 “年工资” , commission_pct from employees;

if null函数本质是将null变为0参与运算。


二、比较运算符

select employee_id , salary from employees where salary < 2400;


三、dual伪表和数值运算

1.常规运算

浮点型转化

字符串隐式转化

字符串和null

字符串隐式转化成0(不包含数字)

除法0值的问题

select 100/0.0,100 div 10 ,100 div 0 from dual;

取余0值问题

select 100 % 3,100%0 from dual;

2.比较运算符

select 1=2,1!=2,1='1',1='a' from dual;

select 'a' = 'a','a'='ab','a'='b' from dual;

空值参与比较运算的问题

select last_name,salary from employees where  COMMISSION_PCT =  NULL;

3.<=>安全相等

select last_name,salary from employees where  COMMISSION_PCT <=>  NULL;

除了<=>之外与null相运算都是null

不相等


四、常用正则相关的比较运算符

1.基本运算符

is null

select last_name,salary from employees where  COMMISSION_PCT is NULL;#

not 取反

select last_name,salary from employees where NOT (COMMISSION_PCT <=> NULL);

等价于is not null

select last_name,salary from employees where  (COMMISSION_PCT IS NOT NULL);

least()和greatest()

select least('a','b','c','e'),greatest('a','b','c','e') from dual;

取两列中最长的字符串和最短的字符串

select least( first_name, last_name ),greatest(first_name, last_name) from employees;

betweent … and 区间

注意区间不能为空,不然输出会有问题

select * from employees where salary between 6000 and 8000 ;

等价于

select * from employees where salary > 6000 and salary < 8000 ;

in和not in

select * from employees where department_id in (10,20,30,40);

等价于

select * from employees where department_id = 10 or department_id = 20 or department_id = 30 or department_id = 40;

not in

select * from employees where department_id not in (10,20,30,40);

2.模糊查询

%:代表不确定个数的字符

查询包含字符’a’的员工信息

查询字符’a’开头的员工信息

查询包含字符’a’和’e’的员工信息

写法1:

写法2:

select last_name from employees where last_name like '%a%e%' or  last_name like '%e%a%' ;

_:占位符一个不确定的字符

select last_name from employees where last_name like '_a%' ;

转义字符

\

查询JOB_ID的第三个字符是_且第四个是a的JOB_ID:

select JOB_ID from employees where JOB_ID like "__\_a%" ;

其他字符做转义,需要告诉系统

$做转义字符:

select JOB_ID from employees where JOB_ID like "__$_a%" escape '$' ;

3.正则表达式

regexp 和 rlike

select "shkstart" regexp "^s","shkstart" regexp "t$","shkstart" regexp "hk" , "shkstart" regexp "ht"from dual;

select "guigui" regexp "gu.gu","guigui" regexp "[gu]" from dual;


五、逻辑运算符

or:

select last_name from employees where last_name like '%a%e%' or  last_name like '%e%a%' ;

and:

select last_name,salary,department_id from employees  where department_id = 50 and salary > 6000;


六、位运算

select 4<<1,8>>1 from dual;


总结

常见运算符基本就是这些了,里面有使用案例,希望大家后面多多练习,毕竟mysql除了懂原理外大量的练习才是王道。

可以点个小👍吗

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
8月前
|
关系型数据库 MySQL Java
MYSQL运算符和分页
MYSQL运算符和分页
46 0
|
1月前
|
SQL 关系型数据库 MySQL
mysql基本查询、运算符、排序和分页
mysql基本查询、运算符、排序和分页
|
8月前
|
SQL 关系型数据库 MySQL
MySql where 查询条件与运算符
MySql where 查询条件与运算符
67 0
|
3月前
|
SQL 安全 关系型数据库
MySQL技能完整学习列表3、SQL语言基础——3、SQL运算符和函数
MySQL技能完整学习列表3、SQL语言基础——3、SQL运算符和函数
38 0
|
3月前
|
关系型数据库 MySQL 数据库
【MySQL进阶之路丨第十七篇(完结)】一文带你精通MySQL运算符
【MySQL进阶之路丨第十七篇(完结)】一文带你精通MySQL运算符
23 0
|
4月前
|
存储 关系型数据库 MySQL
MySQL基础篇(运算符、排序分页、多表查询、函数)-3
MySQL基础篇(运算符、排序分页、多表查询、函数)
51 0
|
4月前
|
SQL 关系型数据库 MySQL
MySQL基础篇(运算符、排序分页、多表查询、函数)-2
MySQL基础篇(运算符、排序分页、多表查询、函数)
40 0
|
4月前
|
SQL 关系型数据库 MySQL
MySQL基础篇(运算符、排序分页、多表查询、函数)-1
MySQL基础篇(运算符、排序分页、多表查询、函数)
70 0
|
4月前
|
关系型数据库 MySQL 数据库
【MySQL进阶之路丨第十七篇(完结)】一文带你精通MySQL运算符
【MySQL进阶之路丨第十七篇(完结)】一文带你精通MySQL运算符
27 0
|
9月前
|
SQL 安全 关系型数据库
MySQL运算符大总结
MySQL运算符大总结