Mysql 常见函数的使用【总结】

使用select 可以直接测试函数:例如:select 'concat';
在这里插入图片描述
下面来介绍一些Mysql常用的函数:

1、concat()函数:把concat内部的参数拼接到一起

  • 例:查询员工姓名和工资,要求工资的单位是元:
    select ename,concat(sal,'元') 工资 from emp;

2、数值计算: + - * / %(mod(7,2)等效7%2)

  • 例:查询商品表中每个商品的单价,库存及总价值(单价*库存)
    select price,num,price * num '总价值' from t_item;
  • 例:查询员工表中每个员工的姓名,工资,及年终奖(五个月的工资)
    select ename,sal,sal * 5 '年终奖' from emp;

3、日期相关函数:

  1. 获取年月日时分秒:select now()
  2. 获取当前日期:select curdate()
  3. 获取当前时间的时分秒:select curtime()
  4. 从年月日时分秒中提取年月日:select date(now())
  5. 从年月日时分秒中提取时分秒:select time(now())
  6. 从年月日时分秒中提取时间分量(年 月 日 时 分 秒):extract()
    • 提取年:select extract(year from now());
    • 提取月:select extract(month from now());
    • 提取日:select extract(day from now());
    • 提取时:select extract(hour from now());
    • 提取分:select extract(minute from now());
    • 提取秒:select extract(second from now());
  • 例:查询员工表中所有员工姓名和入职年份:
    select ename,extract(year from hiredate) from emp;

4、日期格式化:date_format(时间,格式)

  • %Y:四位年,%y:两位年

  • %m:两位月,%c:一位月

  • %d:日,

  • %H:24小时制,%h:12小时制

  • %i:分

  • %s:秒

  • 例:将当前时间转化格式:select date_format(now(),'%Y年%m月%d日%H时%i分%s秒');
    在这里插入图片描述

  • 例:查询商品名称和商品创建日期(格式:x年x月x日)
    select title,date_format(date(curdate_time),'%Y年%m月%d日') from t_item;
    select title,date_format(curdate_time,'%Y年%m月%d日') from t_item;

5、把非标准日期字符串转化为标准的时间格式:str_to_date(时间字符串,字符串格式)

  • 例:14.08.2018 08:00:00:
    select str_to_date('14.08.2018 08:00:00','%m.%d.%Y %H:%i:%s');

6、ifnull(x,y)函数:

说明:age=ifnull(x,18)如果x的值为null则age=18,如果不为null则age=x;

  • 例:修改员工表中奖金为null的值为0:
    update emp set comm = ifnull(comm,0);

7、聚合函数:用于多条数据进行统计

  1. 求和:sum(字段名)
    • 例:查询emp表中10号部门的工资总和:
      select sum(sal) from emp where deptno = 10;
  2. 平均值:avg(字段名)
    • 例:查询emp表中所有员工工资的平均值:
      select avg(sal) from emp;
  3. 最大值:max(字段名)
    • 例:查询30号部门的员工的最高奖金:
      select max(comm) from emp where deptno = 30;
  4. 最小值:min(字段名)
    • 例:查询商品表中价格最便宜的商品单价:
      select min(price) from t_item;
  5. 统计数量:count(数量名) 一般情况下用count(*)
    • 例:统计30号部门有多少人:
      select count(*) from emp where deptno = 30;

8、和字符串相关函数:(小标都是从1开始的,一般都是含头不含尾的)

  1. 获取字符串长度:char_length(str)

    • 例:获取所有员工的姓名和姓名的字符串长度:
      select ename,char_length(ename) from emp;
  2. 获取字符串在另一个字符串中出现的位置:instr(str,substr)

    • select instr('adegc','e');
      在这里插入图片描述
  3. 插入字符串:insert(str,start,length,newStr);

    • select insert('abcdefg',2,4,'m');
      在这里插入图片描述
  4. 转大写:upper(字符串),转小写:lower(字符串)

    • select upper('nba'),lower('NBA');
      在这里插入图片描述
  5. 左边截取:left(str,length),右边截取:right(str,length)

    • select left('abcdefg',2),right('abcdefg',2);
      在这里插入图片描述
  6. 去除两端空白:trim(str)(中间的去除不了)

    • select trim(' a b ');
      在这里插入图片描述
  7. 截取字符串:substring(str,截取开始的位置,截取的长度)(含头不含尾)

    • select substring('abcdefg',3,2);
      在这里插入图片描述
  8. 重复:repeat(str,count) count为重复的次数

    • select repeat('ab',2);
      在这里插入图片描述
  9. 替换:replace(str,old,new)
    select replace('This is mysql','my','your');
    在这里插入图片描述

  10. 反转:reverse(str)

    • select reverse('abc');
      在这里插入图片描述

9、数学相关函数:

  1. 向下取整:floor(num)
    • select floor(3.48);
      在这里插入图片描述
  2. 四舍五入:round(num)round(num,m) m代表小数位数
    • select round(3.84);
      在这里插入图片描述
    • select round(3.8456,3);
      在这里插入图片描述
  3. 非四舍五入(保留几位小数):truncate(num,m)
    • select truncate(3.8456,3);
      在这里插入图片描述
  4. 获取随机数:rand()(取值范围0-1的随机数)
    • 例:求5-10的随机数:
      select floor(rand()*6)+5;
      在这里插入图片描述

    • 例:求3-8的随机数:
      select floor(rand()*6)+3;
      在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44296929/article/details/108580218