玖叶教程网

前端编程开发入门

原来MySQL有这么多函数可以用呀(mysql函数有哪些)

MySQL函数

字符函数

length字节个数

select length('john');

concat拼接字符串

select concat('I',' ','like',' ','mysql');

upper/lower大小写转换

select upper('mysql');
select lower('MySQL');

substr/substring字符串截取

#从第几个字符开始截取,注意:索引从1开始
select substr('I like MySQL',8);
#从第几个字符开始截取,截取几个字符
select substr('I like MySQL',8,2);

instr子串在字符串中的起始索引

select instr('I like MySQL','MySQL');

trim去掉前后空格

select trim(' My SQL ');

lpad用指定字符进行左填充达到指定长度

select lpad('MySQL',8,'*');

rpad用指定字符进行右填充达到指定长度

select rpad('MySQL',8,'*');

replace替换

select replace('aabb','bb','cc');

数学函数

round四舍五入

select round(1.4);
#小数点后保留几位
select round(1.567,2);

ceil向上取整

select ceil(1.2);

floor向下取整

select floor(1.2);

truncate截断(小数点后保留几位)

select truncate(1.61,2);

mod取模

select mod(10,3);

日期函数

now返回当前系统时间

select now();

curdate返回当前系统日期,不包含时间

select curdate();

curtime返回当前系统时间

select curtime();

获取指定的部分

select year('2020-10-10');
select month('2020-10-10');

strtodate 将日期格式的字符转换成指定格式的日期

select str_to_date('2020年10月12','%Y年%m月%d');

date_format将日期转换为字符

select date_format('2020/10/12','%Y-%m-%d');

流程控制

流程控制结构分为顺序结构、分支结构、循环结构

顺序结构

从上往下依次执行

分支结构

从两条或多条分支选择一条执行

if函数 if-else效果

#语法 if(表达式1,表达式2,表达式3)  表达式1成立,则执行表达式2,否则执行表达式3
select if(6<3,'小于','大于');

case函数

相当于switch-case

#语法
case 表达式|变量|字段
  when 要判断的值  then 结果
  when 要判断的值  then 结果
  ...
  else result
end

相当于多重if语句

#语法
case
  when 要判断的条件  then 结果
  when 要判断的条件  then 结果
  ...
  else result
end

if结构

#语法
if 条件1 then 语句1;
elseif 条件2 then 语句2;
else 语句;
end if;

循环结构

在满足一定的条件下,重复执行一段代码

循环控制

  • iterate 类似于continue
  • leave 类似于break

while结构

【标签:】while 循环条件 do
    循环体
end while 【标签】;

loop结构

【标签:】loop
    循环体
end loop 【标签】;

repeat结构

【标签:】 repeat 
    循环体
until 捷顺循环的条件
end repeat 【标签】;

聚合函数

求和

select sum(salary) from employees;

平均值

select avg(salary) from employees;

最大值

select max(salary) from employees;

最小值

select min(salary) from employees;

个数

select count(id) from users;

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言