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



一.数据处理函数/单行处理函数
字符串函数
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(属性列,替换的内容)
select isnull(mark,0.00) as Mark from course;

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


数学函数
1: rand();—生成随机数
select rand() from dual; --dual是一个伪表
select 1+1 from dual;
select rand(); --可以简写
select round(rand()*100);---生成0~100随机数
2: least(value1, value2, …) 返回最小值
select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76);
select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76) as min_value; --列名可以起一个别名
3: greatest(value1, value2, …) 返回最大值
select greatest(54,76,4,65,76,87,87,56,65,654,45,23,1,76);
4: round(M, D); 返回M的四舍五入的值, D表示要保留几们小数,默认值是0
select round(1.69);
select round(1.69, 1);
5: abs() 绝对值
select 5-10;
select abs(5-10);


日期函数
1: now()/sysdate() 获取当前的日期时间
select now(), sysdate(), current_timestamp();
select now(6), sysdate(6), current_timestamp(6);
ps: now(), current_timestamp();没有区别, 表示sql开始执行时的时间
 sysdate()表示这个函数开始时间
2: curdate()获取当前日期

select curdate(); –只有年月日

3: curtime()获取当前时间

select curtime(); –只有时分秒

4: 日期的加运算date_add
select history, date_add(history, interval '1 12:10' day_minute) from employee; --date_add(history, interval '1 12:10' day_minute)
select history, date_add(history, interval '1-1' year_month) from employee;  --date_add(history, interval '1-1' year_month)
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(日期记录, 日期格式)—获取日期的指定部分(把日期转换为指定的格式)
select history, date_format(history, '%Y年%m月%d号') from employee;
select history, date_format(history, '%d号') from employee;
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(‘日期字符串’,‘日期格式’)—中文日期字符串转换日期
insert into employee values(11, '张飞', '男', 22, 3000, '2017年02月01号'); --报错
insert into employee values(11, '张飞', '男', 22, 3000, str_to_date('2017年02月01号', '%Y年%m月%d号 %H时%i分%s秒'));

2)查询
select name, birthday from studnets where birthday = '1997-12-10';---实现自动转型
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中的日期格式化

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

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

  • MySQL默认的日期格式:%Y-%m-%d 如果字符串刚好满足格式 将自动转型

  • str_to_date: 通常用于输入语句; date_format: 通常用于输出语句



二.分组函数/聚合函数/多行处理函数
汇总函数
1: avg();
 select * from employee where sal >= 6000;
 select avg(sal) from employee where sal >= 6000;
2: count()—取得记录数
 select count(*) from employee;---这里会记录null值
 select count(emp_name) from employee;
 select count(sal) from employee;  --这里会忽略null值
 select count(*) from employee where sal >= 4000;
 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 后面==

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值