失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > mysql学习笔记三常用函数(聚合函数 数学函数 字符串函数 日期函数)

mysql学习笔记三常用函数(聚合函数 数学函数 字符串函数 日期函数)

时间:2022-10-21 21:37:13

相关推荐

mysql学习笔记三常用函数(聚合函数 数学函数 字符串函数 日期函数)

聚合函数

count()计算总数,结果排除null

--student表中的数据总数select count(*) from student;

SUM():求和

-- 所有学生语文总成绩select sum(yuwen) as 学生语文总成绩 from student;

AVG():平均值

-- 语文平均成绩select avg(yuwen) as 语文平均成绩 from student;-- 数学平均成绩select avg(shuxue) as 数学平均成绩 from student;

MAX():最大值

select max(yuwen) as 语文最高成绩 from student;select max(shuxue) as 数学最高成绩 from student;

MIN():最小值

select min(yuwen) as 语文最抵成绩 from student;select min(shuxue) as 数学最抵成绩 from student;

数学函数

round(x,y)四舍五入,对x进行四舍五入,y保留几位小学

select round(89.8,0);

abs(x)求x的绝对值

select abs(-20);

其它数学函数还有很多,正弦值,余弦值等。

字符串函数

字符串长度

SELECT LENGTH('lisi');

concat(str,str,str)返回拼接后的字符串,如果有一个为null,返回结果为null。

SELECT CONCAT('a','b','d');SELECT CONCAT('a',null,'d');

contcat_ws(separator,str1,str2,…)contcat_ws() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。

select concat_ws(',','a','b','c'); -- 返回结果为abcselect concat_ws(',','a','b',null); -- 返回结果为 abselect concat_ws(null,'a','b','c');-- 返回结果为null

trim(str)去除str的前后空格

SELECT TRIM(' a '); -- 输出结果为a

replace(str,index,s2)使用字符串s2替换字符串str中所有的字符串index

SELECT replace('zhang', 'z', 'c');-- 输出结果为chang

substring(str,n,len)返回字符串str的子串,起始位置n,长度为len

SELECT substring('zhang',3, 3);-- 返回结果为ang

reverse(str)返回字符串反转后的结果

SELECT REVERSE('lisi');-- 返回结果为isil

LOCATE(subStr,string)判断字符串(string)中是否包含另一个字符串(subStr)

SELECT LOCATE('l','lisi');-- 返回1,代表包含SELECT LOCATE('a','lisi');-- 返回0,代表不包含

LOWER(str)返回str小写

SELECT LOWER('ZHANG');-- 返回zhang

UPPER(str)返回str大写

SELECT UPPER('zhang');-- 返回ZHANG

日期函数

now()获得当前日期+时间(date + time)

select now();

curdate()获取系统当前日期

select curdate();

curtime()获取系统当前时间

select curtime();

sysdate()获取当前系统日期和时间

select sysdate();

current_timestamp()获得当前时间戳

select current_timestamp();

如果觉得《mysql学习笔记三常用函数(聚合函数 数学函数 字符串函数 日期函数)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。