MySQL Advanced in Data Analysis

The previous article introduced the classification of sql and some basic knowledge. This blog will introduce the usage of functions and advanced queries in mysql. Previous blog: Portal .

1. Function

1.1 One-line functions
  • Mathematical function
 # 绝对值
 abs()
 
 # 向上取整,最接近并且大于等于该值的整数值
 ceiling()/ceil()
 
 # 向下取整,最接近并且小于等于该值的整数值
 floor()
 
 # 取模运算(取余)
 mode(m,n)
 
 # 求PI值
 PI()
 
 # 求幂运算
 pow(m,n)
 
 # 随机数
 rand()  
 
 # 四舍五入
 round(m,n)
 
 # 截取m小数点后n位
 truncate(m,n)
  • Character function
 # 获取str的ascii码值
 ascii(str)

 #大小写主要用在验证码问题上
 # 将字符串转换为小写
 lower(字段|表达式)
 # 将字符串转换为大写
 upper(字段|表达式)
 
 # 拼接字符串
 concat(str1,str2…)
 
 # 获取字符串长度
 length(字段|表达式)
 
 # 截取字符串,pos开始位置,从1开始;len表示长度
 substr(str,pos,len)
 
 # 在str中搜索old,使用new代替
 replace(str,old,new)
 
 # str长度不够len,使用s左侧填充
 lpad(str,len,s)
 
 # str长度不够len,使用s右侧填充
 rpad(str,len,s)
 
 # 去除左右两侧的空格,字符串里面的空格不能去除
 trim()
  • Date function
 # 获取当前日期时间
 NOW()/SYSDATE()/CURRENT_TIMESTAMP()
 # 获取当前系统日期
 CURRENT_DATE()/CURDATE()
 # 获取当前系统时间
 CURRENT_TIME()/CURTIME()
 # 获取天数
 DAY(date)
 # 获取月份
 MONTH(date)
 # 获取年份
 YEAR(date)
 # 返回一年中的周数
 week(date)
 # 返回一周中的第几天(0-6)
 weekday(date)
 # 日期计算
 DATE_ADD(date,INTERVAL expr unit)
1.2 Aggregate functions
# 平均值
avg()
# 获取总数
sum()
# 获取最大值
max()
# 获取最小值
min()
# 统计数目
count()   统计记录数或者统计某个非空字段的个数

#注意在select字句中不能出现除聚合函数外的任意字段(没有分组)
#where字句中不能出现聚合函数(聚合函数的执行在where的后面)
1.3 Grouping function
GROUP BY
 	用于将信息划分为更小的组
 	每一组行返回针对该组的单个结果
HAVING子句
 	用于指定 GROUP BY 子句检索行的条件
#查询平均工资大于2000的部门编号
select deptno,avg(sal) avg from emp group by deptno having avg>=2000

Here to explain the difference between having and where, both are used to filter, where is in front of group by, and having after group by
where is to filter the data source, and having is to filter the query results, that is The execution time of where is earlier than having

1.4 Encryption function
分为可逆和不可逆:
可逆:加密/解密   如:java: BASE64.encode()/decode()     python: 爬虫 encode()/decode()

不可逆:只能加密不能解密
select md5('root');
select sha('root');
select password('root');
1.5 Process function
如果expr1是真, 返回expr2, 否则返回expr3
IF(expr1,expr2,expr3)	

如果expr1不是NULL,返回expr1,否则返回expr2
IFNULL(expr1,expr2)	

如果value是真, 返回result1,否则返回default
CASE WHEN [value1] THEN[result1]ELSE[default] END	

如果expr等于value1, 返回result1,否则返回default
CASE [expr] WHEN [value1] THEN[result1]ELSE[default] END	

2. Advanced query

2.1 Connect query
  • Cartesian Product

Combine all possible ordered pairs of records in multiple tables

  • Internal connection

Records that match multiple tables are connected and displayed in the result set

There is no distinction between master and slave tables, regardless of the connection order

# 查询所有雇员及其部门信息
select * from emp,dept where emp.deptno = dept.deptno;
select * from emp inner join dept on emp.deptno = dept.deptno;
select * from emp inner join dept using(deptno); #字段名称必须一致,会自动去除重复列
  • Natural connection

Natural connections are equal connections

The fields with the same field names in the table are connected, and the duplicate columns will be automatically removed

select * from emp natural join dept;
  • Outer join

Based on the driving table, the records are matched in the matching table in turn, if they match, they are connected and displayed in the result set, otherwise they are filled with null

There is a master-slave table, which is related to the connection sequence.

left/right [outer] join … on…

select * from emp 
LEFT JOIN dept
on emp.deptno = dept.deptno
  • Self-connection

Connect itself as a mirror (connect itself to itself)

# 查询员工及其领导的名称
select e1.ename 名称,e2.ename 领导
from emp e1,emp e2
where e1.mgr = e2.empno
2.2 Subquery

Nested query, a query that uses one query result as another query condition or part of the query

# 单行子查询
  #查询工资大于7788号员工的所有员工信息
  select * from emp where sal >(select sal from emp where empno =7788);
# 多行子查询
  返回多值可以使用anyall来修饰。
  =any相当于in,<any小于最大值,>any大于最小值;
  <>all相当于not in,>all大于最大值,<all小于最小值。
  
# 查询超过所在部门平均工资的员工信息
select * from emp e1 where sal > 
(select avg(sal) from emp e2 where e1.deptno = e2.deptno);

#查询薪水大于2000的部门名称
select dname from dept d where deptno in
(select deptno from emp e where sal > 2000);select dname from dept d where EXISTS 
(select * from emp e where sal > 2000 and d.deptno=e.deptno);
# in和exists的区别
in先执行子查询,在执行主查询;exists先执行主查询;
exists子查询不返回具体结果,返回true值出现在结果集,否则不出现。
2.3 Joint query

union: union, all contents are queried, and displayed repeatedly
union all: union, all contents are displayed, including duplicates

select * from emp where deptno = 20
UNION ALL
select * from emp where sal <=2000

3. Summary

Here are some functions and query methods that need to be mastered in sql

Guess you like

Origin blog.csdn.net/weixin_45696161/article/details/106384059