Mysql-内置函数

一.什么是函数?

函数是指一段可以直接被另外一段程序调用的程序或代码。

mysql内置了很多的函数,我们只需要调用即可。

二.字符串函数

MySQL中内置了很多字符串函数:

三.根据需求完成以下SQL编写

由于业务需求变更,企业员工的工号,统一为5位数,目前不足5位数的全部在前面补0。比如:1号员工的工号应该为00001

update emp set workno=lpad(workno,5,'0')

四.数值函数

ROUND,X是四舍五入,y是保留几位小数。

select round(2.34,2)

对2.34进行四舍五入,保留两位小数。

select round(2.345,2) //2.35
select round(2.344,2) //2.34

五.通过数据库的函数,生产一个六位数的随机验证码

select  lpad( round(rand()*1000000 //790714.4290449233,0),6,'0');

0.019255 //19255 因为生成的数字是(0,1)之间。

六.日期函数

七.查询所有员工的入职天数,并根据入职天数倒序排序

select  name ,datediff(curdate(),entrydate) as 'entrydays' from emp order by entrydays desc;

八.流程函数

流程函数也是常用的一类函数,可以在SQL语句实现条件筛选,从而提高语句的效率。

九.查询emp表的员工姓名和工作地址

select

     name

   case  workaddress   when '北京' then '返回一线城市' when '上海' 
then '一线城市' else '二线城市' end) as '工作地址'

from  emp;

十.查询分数的案例

>=85 展示优秀

>=60 展示及格

否则,展示不及格

select
id,
name,
( case when math>=85 then '优秀'  when math>=60 then '及格' end) '数学',
( case when english>=85 then '优秀'  when math>=60 then '及格' end) '英语',
( case when chinese>=85 then '优秀'  when math>=60 then '及格' end) '语文',
from score;

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 22:52:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 22:52:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 22:52:02       58 阅读
  4. Python语言-面向对象

    2024-07-12 22:52:02       69 阅读

热门阅读

  1. 递归函数遍历格式化字典

    2024-07-12 22:52:02       21 阅读
  2. 【LeetCode】2089. 找出数组排序后的目标下标

    2024-07-12 22:52:02       21 阅读
  3. 简谈设计模式之单例模式

    2024-07-12 22:52:02       19 阅读
  4. Linux文件系统

    2024-07-12 22:52:02       17 阅读
  5. 进程的阻塞

    2024-07-12 22:52:02       24 阅读
  6. 连接docker私有仓库

    2024-07-12 22:52:02       21 阅读
  7. React中的useCallback

    2024-07-12 22:52:02       19 阅读
  8. 【力扣C语言】每日一题—第50题,Pow(x,n)

    2024-07-12 22:52:02       22 阅读