SEC8 - MySQL 查询语句--------------进阶4:常见的函数

时间:2022-12-18 20:11:56
# 进阶4:常见的函数
/*
概念:将一组逻辑语句封装在方法体中,对外暴露方法名
好处:1.隐藏了实现细节 2.提高代码的复用性
调用: select 函数名() 【from 表】;
特点:
(1)叫什么(函数名)
(2)干什么(函数功能)
分类:
1.单行函数
字符函数,数学函数,日期函数,其他函数,流程控制函数
2.分组函数
常见函数总结:
1.单行函数 字符函数:
length,concat,substr,instr,trim,upper,lower,lpad,rpad,replace 数学函数:
round,ceil,floor,truncate,mod 日期函数
now,curdate,curtime,year,month,monthname,day,hour,minute,second,str_to_date,date_format 其他函数:
version(),databases() 流程控制函数
if,case 2.分组函数 */ # 一、字符函数
#1. length获取参数值的字节个数, utf8下面一个字母占一个字节,一个汉字占3个字节
select length("john");
select length("张三丰hahaha"); show variables like "%char%"; #2.concat拼接字符串
select concat(first_name," ",last_name) from employees; #3.upper小写变大写、lower大写变小写
select upper("john");
select lower("JOHN"); select lower("JohN");
#示例:将姓变大写,名变小写,然后拼接
select concat(lower(first_name)," ",upper(last_name)) from employees; #4. substr,substring
# 索引从1开始 substr("字符串",num,length),num为截取开始的位置,length为截取的长度,可选
select substr("我的名字叫小芳",6);
select substr("我的名字叫小芳",3,2); #案例:将姓名中首字符大写,其他字符小写,然后用_拼接显示
select concat(upper(substr(first_name,1,1)),lower(substr(first_name,2)),"_",last_name) from employees; #5.instr 返回字串第一次出现的索引,如果找不到则返回0
select instr("我的名字叫小芳","小芳"); #6.trim 去删掉前后的重复
select trim(" aaa ") as out_put; #获得aaa
select trim("b" from "bbbbbbbbaaaaavbfddsaaabbbb") as out_put;
#获得aaaaavbfddsaaa #7.lpad 用指定的字符实现左填充
#8.rpad 用指定的字符实现右填充 #9.replace替换
select replace("wqq123wqqhahahahwqq","wqq","ttt") as out_put; #二、数学函数
# 1.round 四舍五入
select round(-1.55);
select round(-1.566,2);#小数点后保留2位 #2. ceil 向上取整,返回>=该参数的最小整数
select ceil(1.02); # floor向下取整
select floor(1.02); #4.truncate截断
select truncate(1.69999,1); #1.6,小数点后1位 #5.mod取余等价于%
# 返回与被除数有关
#mod(a,b) a-a/b*b # 三、日期函数
# 1. now 返回当前系统的日期和时间
select now(); # 2. curdate 返回当前系统的日期,不包含时间
select curdate(); # 3. curtime 返回当前系统的时间,不包含日期
select curtime(); # 4. 可以获取指定的部分,年,月,日,小时,分,秒
select year(now()) as 年; #年 select month(now()); #月
select monthname(now()); #英文月 # str_to_date:将日期格式的字符转换为日期
select str_to_date("1998-3-2","%Y-%c-%d") as out_put; # 查询入职日期位1992-4-3的员工信息
select * from employees where hiredate="1992-4-3"; # date_format:将日期转换为字符 #四、其他函数 select version();
select database(); #五、流程控制函数
#1.if 函数:if else的效果
select if(10<5,"大","小");
use myemployees;
#案例:显示姓名和奖金率,有奖金哈哈,无奖金呵呵
select last_name,commission_pct, if(commission_pct is null,"没奖金,呵呵","有奖金,哈哈") as 备注 from employees; #2.case函数的使用1-----switch case的效果
/*
case 要判断的字段或者表达式
when 常量1 then 要显示的值1 或者 语句1;
when 常量2 then 要显示的值2 或者 语句2;
...
else 要显示的值n 或者 语句n;
end
*/ /*案例:查询员工的工资,要求
部门号=30,显示工资的1.1倍
部门号=40,显示工资的1.2倍
其他部门,显示工资的1.3倍
*/
select salary as "原始工资",department_id,
case manager_id
when 30 then salary*1.1
when 40 then salary*1.2
else salary*1.3
end as "新工资" from employees; #case函数的使用2
/*
case
when 条件1 then 要显示的值1 或者 语句1;
when 条件2 then 要显示的值2 或者 语句2;
...
else 要显示的值n 或者 语句n;
end
*/ #案例:查询员工的工资情况
/*
如果工资>20000,显示A级
如果工资>15000,显示B级
如果工资>10000,显示C级
否则,显示D级
*/
#以上非等值判断
select salary,case
when salary>20000 then "A"
when salary>15000 then "B"
when salary>10000 then "C"
else "D"
end as "工资等级" from employees; #题目1:显示系统时间(注:日期+时间)
select now(); #题目2;查询员工的工号,姓名,工资,以及工资提高20%之后的结果(now salary)
select last_name,employee_id,salary,salary*1.2 as "now salary" from employees; #题目3:将员工的姓名按照首字母排序,并写出姓名的长度(length)
select last_name,length(last_name) from employees order by substr(last_name,1,1);
#注意 order by 的位置 # 题目4:做一个查询,产生下面的结果
<last_name> earns <salary> monthly but wants <salary*3> select concat(last_name," earns ",salary," monthly but wants ",salary*3) as "dream salary" from employees; #题目5:使用case-when,按照下面的条件
job grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
产生 last_name ,job_id,grade select distinct job_id from employees; select job_id as "job", case job_id
when "AD_PRES" then 'A'
when "ST_MAN" then 'B'
when "IT_PROG" then 'C'
when "SA_REP" then "D"
when "ST_CLERK" then "E"
end as "grade" from employees; #2.分组函数
/*
功能:用作统计使用,又称为聚合函数或者统计函数或者组函数
输入多行,输出一行 分类:
sum,avg,max,min,count 特点:
1.sum,avg:数值型
max,min,count:数值型,字符型
2.是否忽略NULL值
以上所有分组函数均忽略null值
3.可以和distinct搭配 4.count函数的详细介绍(这个不忽略null值的)
一般使用count(*)统计行数 5.和分组函数一同查询的字段有限制
一般要求是group by后的字段
*/ # 1. 简单的使用
select sum(salary) from employees;
select avg(salary) from employees;
select min(salary) from employees;
select max(salary) from employees;
select count(salary) from employees; select sum(salary)as "和", avg(salary) as "平均",min(salary) as "最小", max(salary) as "最大",count(salary) as "总数" from employees;
#round保留小数位
select sum(salary)as "和", round(avg(salary),2) as "平均",min(salary) as "最小", max(salary) as "最大",count(salary) as "总数" from employees; #2.参数支持哪些类型:
sum,avg:数值型
max,min,count:数值型,字符型
count值计算非空的字段 select count(commission_pct) from employees; #35个数据,null值不算
select count(salary) from employees; #107个数据。 #3.是否忽略NULL值
select sum(commission_pct), avg(commission_pct),sum(commission_pct)/35,sum(commission_pct)/107 from employees;
#sum,avg:null不参与计算,直接忽略
select max(commission_pct), min(commission_pct)from employees;
#max,min:null不参与计算,直接忽略 #4.和distinct搭配
select sum(distinct salary),sum(salary) from employees;
#397900,691400 select count(distinct commission_pct),count(commission_pct) from employees;
# 7, 35 #5.count函数的详细介绍
select count(salary) from employees;#107行 select count(*) from employees;#统计行数:107行 select count(1) from employees;#统计行数:107行,与count(*)等价
效率问题:
myism存储引擎下,count(*)效率最高
innodb存储引擎下,count(*)和count(1)效率差不多,但是比count(字段)效率高 #6. 和分组函数一同查询的字段有限制 select avg(salary),commission_pct from employees;
这样是没有意义的,avg(salary)是一个数据,commission_pct是107个数据。 #题目1:查询员工表中的最大入职时间和最小入职时间的相差天数(difference)
select max(hiredate)-min(hiredate) as "difference" from employees;#表达错误 select datediff(max(hiredate),min(hiredate)) as "difference" from employees; select datediff(now(),"2000-01-01");#求相差天数 #题目2:查询部门编号位90的员工个数
select count(*) from employees where department_id=90;

  

SEC8 - MySQL 查询语句--------------进阶4:常见的函数的更多相关文章

  1. MySQL 查询语句--------------进阶9:联合查询

    #进阶9:联合查询 /* union 联合 合并:将多条查询语句的结果合并成一个结果 语法: 查询语句1 union 查询语句2 union..... 应用场景:要查询的结果来自于多个表,且多个表没有 ...

  2. MySQL 查询语句--------------进阶7:子查询

    #进阶7:子查询 /* 含义: 出现在其他语句中的select语句,称为子查询或者内查询 外部的查询语句,称为主查询或外查询 分类: 按照子查询出现的位置: select后面:只支持标量子查询 fro ...

  3. SEC7 - MySQL 查询语句--------------进阶3:排序查询

    # 进阶3:排序查询 /* 引入: select * from employees; 语法: select 查询列表 from 表 [where 筛选条件] order by 排序的列表 asc/de ...

  4. MySQL 查询语句--------------进阶6:连接查询

    #进阶6:连接查询 /* 含义:多个表格连接,当查询的字段来自于多个表时候,就会用到连接查询 我觉得这里类似于excel中的vlookup函数 笛卡尔乘积现象:表1有m行,表2有n行,结果有m*n行 ...

  5. MySQL 查询语句--------------进阶5:分组查询

    #进阶5:分组查询 /* select 分组函数,列(要求出现在group by的后面) from 表 [where 筛选条件] group by 分组的列表 [order by 子句] 注意: 查询 ...

  6. SEC6 - MySQL 查询语句--------------进阶2:条件查询

    # 进阶2:条件查询 /* 语法: select 查询列表 from 表名 where 筛选条件; 分类: 一.按照条件表达式筛选 条件运算符:> < = !=(等价于<>) ...

  7. SEC5 - MySQL 查询语句--------------进阶1:基础查询

    # 进阶1:基础查询 /* 语法: select 查询列表 from 表名: 特点: 1.查询列表可以是:表中的字段.常量值.表达式.表达式.函数 2.查询的结果是一个虚拟的表格 如何执行:执行谁,请 ...

  8. MySQL 查询语句--------------进阶8:分页查询

    #进阶8:分页查询 /* 应用场景:要显示的数据,一页显示不全,需要分页提交sql请求 语法: select 查询列表 from 表 [join type] join 表2 on 连接条件 [wher ...

  9. MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介

    网站或服务的性能关键点很大程度在于数据库的设计(假设你选择了合适的语言开发框架)以及如何查询数据上. 我们知道MySQL的性能优化方法,一般有建立索引.规避复杂联合查询.设置冗余字段.建立中间表.查询 ...

随机推荐

  1. CSS盒子模型的一些理解

    盒子模型相当于把现实中的盒子形象化. 盒子模型的大小="内容(content)+内填充(padding)+边框(border)+外边距(margin)" 盒子模型方向为:top, ...

  2. 基于vitamio的网络电视直播源码

    这个项目是基于vitamio的网络电视直播源码,也是一个使用了vitamio的基于安卓的网络直播项目源码,可能现在网上已经有很多类似这样的视频播放应用了,不过这个还是相对来说比较完整的,希望这个案例能 ...

  3. 30条MySQL优化总结

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  4. C插入排序

    #include "stdio.h" int main() { ,,,,,}; int i,j; ;j<]);j++) { int key = a[j]; ;i>=&a ...

  5. OpenCV2马拉松第22圈——Hough变换直线检測原理与实现

    计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g/article/details/27220445 收入囊中 Hough变换 概率Ho ...

  6. Controller返回值类型ActionResult

    在mvc中所有的controller类都必须使用"Controller"后缀来命名 并且对Action也有一定的要求: 必须是一个public方法 必须是实例方法 没有标志NonA ...

  7. win32 调用多媒体函数PlaySound&lpar;&rpar;

    必须引入此头文件 #include <mmsystem.h>#pragma comment(lib, "WINMM.LIB") /*------------------ ...

  8. POJ 2031 Building a Space Station 最小生成树模板

    题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...

  9. Maven使用(一)—— Maven的安装与全局配置

    一.Maven安装 Maven的安装步骤: 1.Maven官网(http://maven.apache.org/)下载压缩包,解压缩,当前最新版本是apache-maven-3.5.3-bin.zip ...

  10. Spring MVC启动过程(1):ContextLoaderListener初始化

    此文来自https://my.oschina.net/pkpk1234/blog/61971 (写的特别好)故引来借鉴 Spring MVC启动过程 以Tomcat为例,想在Web容器中使用Spirn ...