常用函数

日期相关函数

函数

函数 说明
curdate() 获取当前日期,格式是:年月日
curtime() 获取当前时间 ,格式是:时分秒
sysdate()/now() 获取当前日期+时间,格式是:年月日 时分秒
year(date) 返回date中的年份
month(date) 返回date中的月份
day(date) 返回date中的天数
hour(date) 返回date中的小时
minute(date) 返回date中的分钟
second(date) 返回date中的秒
date(date) 从年月日时分秒中 提取年月日
time(date) 从年月日时分秒中 提取时分秒
date_format(date,format); 日期格式化函数
%Y 四位年; %y 两位年;
%m 两位月; %c 一位月;
%d 日;
%H 24小时; %h 12小时;
%i 分;
%s 秒;
select str_to_date(非标准格式的时间,格式) 把非标准格式的时间转成标准格式

实例

显示指定字符串

select 'holleworld';

获取当前日期+时间 now()

select now();

获取当前的日期 current:当前

select curdate();

获取当前的时间

select curtime();

从年月日时分秒 提取年月日 和 提取时分秒

select date(now());
select time(now());

从年月日时分秒中 提取 年,月,日,时,分,秒

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

把now()转换成 2018年06月19日 16时27分30秒的格式

select date_format(now(),'%Y年%m月%d日%H时%i分%s秒');

把 14.08.2008 08:00:00 转成标准格式

select str_to_date('14.08.2008 08:00:00','%d.%m.%Y %h:%i:%s');

ifunll()函数

age = ifunll(x,y) 如果x的值为null,则age=y, 如果x的值不位null,则age=x

将emp表中奖金为null的全部修改为0

select comm from emp;
update emp set comm=ifnull(comm,0);

聚合函数

函数 说明
sum(字段名) 求和
avg(字段名) 平均值
max(字段名) 最大值
min(字段名) 最小值
count(*) 统计数量

字符串相关函数

函数 说明
concat(s1,s2..) 将s1,s2 等多个字符串合并为一个字符串
concat_ws(x,s10,f,s2..) 同CONCAT(s1,s2,..)函数,但是每个字符串之间要加上x,x是分隔符
char_length(str) 获取字符串的长度
instr(str,substr)
locate(substr,str)
获取字符串在另一个字符串中出现的位置 从1开始, (substr在str中的位置)
insert(str,start,length,newchar) 插入字符串
str:元字符串
start:开始
length:长度
newchar:新字符串
upper(str) 转大写
lower(str) 转小写
转大小写
left(str,count) 从左边截取
right(str,count)从右边截取
截取字符串, 截取左边count个或者右边count个字符串
trim(str) 去字符串两端的空格
substring(str, start, length);
substring(str, start);
截取字符串, 从第start个开始, length个长度, 或者从第start个开始到最后
repeat(str, count); 重复, 把str字符串重复count次
replace(str,old,new); 替换, 把str中的old全部换位new
reverse(str); 翻转, 把str翻转, 例如'abc' 变为'cba'

数学相关的函数

函数 说明
floor(num); 向下取整,就是舍去小数部分
round(num) 四舍五入
round(num,m) 四舍五入, m代表小数位数
truncate(num,m) 保留m为小数,但不四射五入
rand() 随机数 获取0~1的随机数

随机数联系:

获取3~8的随机数

select floor(rand()*6+8);

获取8~10的随机数

select floor(rand()*3+8);
posted @ 2020-05-31 13:59  zpk-aaron  阅读(574)  评论(0编辑  收藏  举报