MySQL知识点总结(三)---数据处理函数/单行处理函数

阳光穿透心脏的1/2处 2022-05-17 11:19 368阅读 0赞

        • 一.数据处理函数/单行处理函数

          • 字符串函数

            • 1: left(str, len) 返回字符串str的左端len个字符
            • 2: length(str)
            • 3: lower(str)—返回小写的字符串str
            • 4: upper(str) 返回大写的字符串str
            • 4: substr(属性列,起点位置,截取长度) —取子字符串
            • 5: concat() —字符串拼接
            • 6: replace(替换
            • 7.trim(str)—去空格
          • *6:isnull(属性列,替换的内容)
          • 数学函数
          • 1: rand();—生成随机数
          • 2: least(value1, value2, …) 返回最小值
          • 3: greatest(value1, value2, …) 返回最大值
          • 4: round(M, D); 返回M的四舍五入的值, D表示要保留几们小数,默认值是0
          • 5: abs() 绝对值
          • 日期函数
          • 1: now()/sysdate() 获取当前的日期时间
          • 2: curdate()获取当前日期
          • 3: curtime()获取当前时间
          • 4: 日期的加运算date_add
          • 5: 日期的减运算data_sub
          • 6: datediff( sysdate(), 过去的时间记录 )计算日期差
          • *7: date_format(日期记录, 日期格式)—获取日期的指定部分(把日期转换为指定的格式)
          • 8: dayname(时间记录) 计算出一个日期是星期几
          • *9: str_to_date(‘日期字符串’,‘日期格式’)—中文日期字符串转换日期
        • 二.分组函数/聚合函数/多行处理函数

          • 汇总函数
          • 1: avg();
          • 2: count()—取得记录数
          • 3: sum()
          • 4: min()
          • 5: max()

一.数据处理函数/单行处理函数

字符串函数
1: left(str, len) 返回字符串str的左端len个字符

select left('abcdefg', 5);

2: length(str)

select length('abcdefg');

3: lower(str)—返回小写的字符串str

select lower(name) as lowerName from students;

4: upper(str) 返回大写的字符串str

select upper(name) as upperName from students;

4: substr(属性列,起点位置,截取长度) —取子字符串

mysql select id,upper(substr(name,1,3)) as ShortName from city where id between 1000 and 1020; select id from city where substr(name,2,1) = 'a';

5: concat() —字符串拼接

select concat(emp_name, '员工') from employee;

6: replace(替换

select replace(emp_name, '李', '老') from employee where emp_name = '李四';

7.trim(str)—去空格

mysql select id from city from where name = trim(' Reformat ')

*6:isnull(属性列,替换的内容)
  1. select isnull(mark,0.00) as Mark from course;

==数值运算前通常要给目标列进行空的转换==

数学函数
1: rand();—生成随机数
  1. select rand() from dual; --dual是一个伪表
  2. select 1+1 from dual;
  3. select rand(); --可以简写
  4. select round(rand()*100);---生成0~100随机数
2: least(value1, value2, …) 返回最小值
  1. select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76);
  2. select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76) as min_value; --列名可以起一个别名
3: greatest(value1, value2, …) 返回最大值
  1. select greatest(54,76,4,65,76,87,87,56,65,654,45,23,1,76);
4: round(M, D); 返回M的四舍五入的值, D表示要保留几们小数,默认值是0
  1. select round(1.69);
  2. select round(1.69, 1);
5: abs() 绝对值
  1. select 5-10;
  2. select abs(5-10);
日期函数
1: now()/sysdate() 获取当前的日期时间
  1. select now(), sysdate(), current_timestamp();
  2. select now(6), sysdate(6), current_timestamp(6);
  3. ps: now(), current_timestamp();没有区别, 表示sql开始执行时的时间
  4. sysdate()表示这个函数开始时间
2: curdate()获取当前日期

select curdate(); –只有年月日

3: curtime()获取当前时间

select curtime(); –只有时分秒

4: 日期的加运算date_add
  1. select history, date_add(history, interval '1 12:10' day_minute) from employee; --date_add(history, interval '1 12:10' day_minute)
  2. select history, date_add(history, interval '1-1' year_month) from employee; --date_add(history, interval '1-1' year_month)
  3. select history, date_add(history, interval '1' second) from employee; --date_add(history, interval '1' second)
5: 日期的减运算data_sub

select history, date_sub(history, interval '1-1' year_month) from employee;

6: datediff( sysdate(), 过去的时间记录 )计算日期差

select history, sysdate(), datediff(sysdate(), history) from employee; --以天数来表示

*7: date_format(日期记录, 日期格式)—获取日期的指定部分(把日期转换为指定的格式)
  1. select history, date_format(history, '%Y年%m月%d号') from employee;
  2. select history, date_format(history, '%d号') from employee;
  3. select history, date_format(history, '%Y年%m月%d号 %H时%i分%s秒') from employee;
8: dayname(时间记录) 计算出一个日期是星期几

select history, dayname(history) from employee;

*9: str_to_date(‘日期字符串’,‘日期格式’)—中文日期字符串转换日期
  1. insert into employee values(11, '张飞', '男', 22, 3000, '2017年02月01号'); --报错
  2. insert into employee values(11, '张飞', '男', 22, 3000, str_to_date('2017年02月01号', '%Y年%m月%d号 %H时%i分%s秒'));
  3. 2)查询
  4. select name, birthday from studnets where birthday = '1997-12-10';---实现自动转型
  5. select name, birthday from students where birthday = str_to_date('12-10-1997',"%m-%d-%Y")

​ ps: 如果是h则表示12小制, 如果是大H则表示24小明制;

每一个数据库都有自己处理时间的机制,而且世纪开发中一旦使用了date类型将无法,在无法Java中运行,所以通常都是用 ‘日期字符串’ 来进行处理—->最常用的是:str_to_date() date_format()_

  • java中的日期格式

    yyyy年 MM月 dd日 HH小时 mm分 ss秒 SSS毫秒

  • java中的将字符串转换成日期格式

    java SimpleDataFormat sdf=new SimpleDataFormat("yyyy-MM-dd"); Data data =sdf .parse ("1997-12-10");

  • java中的日期格式化

    1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
    2. Date nowTime = new Date();
    3. String temp = sdf.format(nowTime);
  • MySQL的日期模式

    %Y年 %m月 %d日 %H时 %i分 %s秒

  • MySQL默认的日期格式:%Y-%m-%d 如果字符串刚好满足格式 将自动转型
  • str_to_date: 通常用于输入语句; date_format: 通常用于输出语句

二.分组函数/聚合函数/多行处理函数

汇总函数
1: avg();
  1. select * from employee where sal >= 6000;
  2. select avg(sal) from employee where sal >= 6000;
2: count()—取得记录数
  1. select count(*) from employee;---这里会记录null
  2. select count(emp_name) from employee;
  3. select count(sal) from employee; --这里会忽略null
  4. select count(*) from employee where sal >= 4000;
  5. select count(*) from employee where sal <= 4000 or sal is null;
3: sum()

select sum(sal) from employee where sal >= 6000;

4: min()

select min(sal) from employee;

5: max()

select max(sal) from employee;

== 分组函数不能直接使用在where 后面==

发表评论

表情:
评论列表 (有 0 条评论,368人围观)

还没有评论,来说两句吧...

相关阅读

    相关 MySQL数据处理函数

    函数 与其他大多数计算机语言一样,SQL支持利用函数来处理数据。函数 一般是在数据上执行的,它给数据的转换和处理提供了方便。 **大多数SQL实现支持以下类型的函...

    相关 js函数知识总结

    1、函数 什么是函数 : 函数就是一个工具 函数就用来代表某个功能的一段代码块,一个函数就是一个功能 、工具 一般反复使用的功能写到一个函数中 2、函数定义