MySQL数据库原理与实践(五):内置函数
本篇目标了解 MySQL 内置函数的概念及作用掌握函数的基本使用方式。掌握常用字符串函数、数值函数、日期函数的使用方法能够对数据进行简单处理。掌握聚合函数的使用能够完成数据统计与分析操作。掌握条件判断函数的使用实现 SQL 中的数据逻辑处理。能够在实际 SQL 查询中灵活运用内置函数提高数据查询和处理能力。一.函数1.日期函数1.1.介绍与简单使用函数名称描述current_date()当前日期current_time()当前时间current_timestamp()当前时间戳date(datetime)返回 datetime 参数的日期部分date_add(date, interval d_value_type)在 date 中添加日期或时间interval 后的数值单位可以是year、month、day、hour、minute、seconddate_sub(date, interval d_value_type)在 date 中减去日期或时间interval 后的数值单位可以是year、month、day、hour、minute、seconddatediff(date1, date2)两个日期的差单位是天now()当前日期时间使用案例1.获得年月日select current_date();2.获得时分秒select current_time();3.获得时间戳也就是具体的时间select current_timestamp();select now();注意在 MySQL 中current_timestamp()和now()返回值和功能基本一致主要区别在于前者属于 SQL 标准函数后者是 MySQL 中更常用、更简洁的写法。4.在日期的基础上加日期:select date_add(2017-10-28, interval 10 day); select date_add(2017-10-28, interval 10 year); select date_add(2017-10-28, interval 10 month);5.在日期的基础上减去时间select date_sub(2027-10-1, interval 2 day); select date_sub(2027-10-1, interval 2 month); select date_sub(2027-10-1, interval 2 year);6.计算两个日期之间相差多少天:select datediff(2027-10-10, 2066-9-1);1.2.案例演示1.创建一张表记录生日create table tmp( id int primary key auto_increment, birthday date );insert into tmp (birthday) values(current_date());2.创建一个留言表create table msg( id int primary key auto_increment, content varchar(30) not null, sendtime datetime );insert into msg(content,sendtime) values(hello1, now()); insert into msg(content,sendtime) values(少偶好甜, now());当我们想要显示所有留言信息发布日期只显示日期不用显示具体时间时select content,date(sendtime) from msg;如果是查询在2分钟内发布的帖子可以如图理解insert into msg(content,sendtime) values(少偶99, now()); select * from msg where date_add(sendtime, interval 2 minute) now();2.字符串函数2.1.介绍函数名称描述charset(str)返回字符串的字符集concat(string1 [, string2, ...])连接多个字符串instr(string, substring)返回substring在string中首次出现的位置未找到返回0ucase(string)将字符串转换为大写lcase(string)将字符串转换为小写left(string, length)从字符串左边截取length个字符length(string)返回字符串的长度字节数replace(str, search_str, replace_str)将字符串str中的search_str替换为replace_strstrcmp(string1, string2)逐字符比较两个字符串的大小substring(str, position [, length])从str的position位置开始截取length个字符省略length时截取到末尾ltrim(string)去除字符串左侧空格rtrim(string)去除字符串右侧空格trim(string)去除字符串两端空格2.2.使用实例注意此次出现的表是之前已经创建好了的1.获取emp表的ename列的字符集:select charset(ename) from emp;2.要求显示exam_result表中的信息显示格式“XXX的语文是XXX分数学XXX分英语XXX分”select concat(name, 的语文是,chinese,分数学是,math,分) as 分数 from exam_result;3.求学生表中学生姓名占用的字节数:select length(name), name from exam_result;注意length函数返回字符串长度以字节为单位如果是多字节字符则计算多个字节数 如果是单字节字符则算作一个字节。比如字母数字算作一个字节中文表示多个字节数 与字符集编码有关例如4.将emp表中所有名字中有S的替换成上海:select replace(ename, s, 上海) ,ename from emp;5.字符串转大小写select ucase(ASDddf); select ucase(aaaddf); select lcase(ASDFGH); select lcase(ASsdgsGH);6.截取emp表中ename字段的第二个到第三个字符:select substring(ename, 2, 2), ename from emp;7.以首字母小写的方式显示所有员工的姓名:8.去除字符串空格select ltrim( sdssf); select rtrim(sdssf ); select rtrim( sdssf );8.逐字符比较两个字符串的大小:select strcmp(fgdsf,dsdsd);3.数学函数1.介绍函数名称描述abs(number)返回绝对值bin(decimal_number)将十进制数转换为二进制hex(decimal_number)将十进制数转换为十六进制conv(number, from_base, to_base)在不同进制之间转换ceiling(number)向上取整floor(number)向下取整format(number, decimal_places)格式化数字并保留指定的小数位数rand()返回[0.0, 1.0)范围内的随机浮点数mod(number, denominator)取模求余数2.使用案例1.绝对值:select abs(-1); select abs(-100);2.进制转换select bin(10); select bin(100);select hex(16); select hex(160);select conv(10,10,2); //10进制到二进制的转换 select conv(10,10,3); //10进制到三进制的转换 select conv(10,10,16); //10进制到十六进制的转换3.取整:select ceiling(23.04); select floor(23.04);4.保留2位小数位数小数四舍五入):select format(12.3456, 2); select format(3.1415926, 2); select format(3.1415926, 10);5.产生随机数:select rand(); select rand();想要较大的值直接乘以10的倍数即可select rand()*1000;6.取模求余数:select mod(10,2); select mod(10,3); select mod(100,5.6);4.其它函数1.user() 查询当前用户:select user();2.md5(str)对一个字符串进行md5摘要摘要后得到一个32位字符串:select md5(admin);3.database()显示当前正在使用的数据库:select database();4.password()函数MySQL数据库使用该函数对用户加密:select password(root);5.ifnullval1 val2 如果val1为null返回val2否则返回val1的值:select ifnull(abc, 123); select ifnull(null, 123);总结本篇我们学习了 MySQL 中常用的内置函数包括日期函数、字符串函数、数学函数以及其它常用函数。日期函数主要用于获取和处理日期、时间数据能够方便地完成时间计算、日期格式化等操作字符串函数可以对字符串进行拼接、截取、替换、大小写转换等处理数学函数提供了绝对值、取整、随机数、进制转换等常见数学运算此外还学习了user()、database()、md5()、ifnull()等实用函数在实际开发中能够帮助我们快速完成数据处理和业务逻辑。熟练掌握这些内置函数不仅能够简化 SQL 语句提高开发效率还能减少应用层代码的编写使数据处理更加灵活、高效为后续学习 MySQL 的高级查询、视图、存储过程等内容打下坚实的基础。