MySQL函数

作者: 墨线宝 | 来源:发表于2021-02-12 21:40 被阅读0次

    原文链接http://zhhll.icu/2020/%E6%95%B0%E6%8D%AE%E5%BA%93/%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E5%87%BD%E6%95%B0/

    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');
    

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

    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;
    

    由于本身的博客百度没有收录,博客地址http://zhhll.icu

    相关文章

      网友评论

        本文标题:MySQL函数

        本文链接:https://www.haomeiwen.com/subject/cuyjxltx.html