1、字符串函数
concat(s1,s2,s3,……)将s1,s2,s3,……多个字符串拼接为1个字符串
select concat('aa','bb','cc');
select concat('aa','bb','cc') from dual;
select concat('雇员编号为:',empno,'的雇员姓名是:',ename) 信息
注意:dual表是mysql提供的一个虚拟表,主要是为了满足
select...from...语法习惯,一般测试时使用, 无实际意义。
lower(s) 将字符串s中的内容转换为小写字母
select lower('Hello World!') from dual;
upper(s) 将字符串s中的内容转换为大写字母
select upper('Hello World!') from dual;
length(s) 测试s字符串的长度
select length('hello world!') from dual;
reverse(s) 将字符串s反转
select reverse('hello') from dual;
trim(s) 去掉字符串s的前后空格
select trim(' hello world! ') from dual;
还有ltrim(),rtrim(),去除左边或右边的空格
replace(s,s1,s2)将s字符串中的s1替换为s2
select replace('hello world!','0','xxhh') from dual;
repeat(s,n)将字符串s重复n次后返回
select repeat('hello world!',3) from dual;
lpad(s,len,s1)在字符串s的左边填充s1,直到长度为len
select lpad('hello',10,'#')from dual;
还有
select rpad('hello',10,'x')from dual;
substr(s,j,len)从字符串s的第j个位置开始取len个字符
select substr('hello',2,3) from dual;
2、数值函数
ceil(n)返回大于n的最小整数
select ceil(10.1) from dual;
floor(n)返回小于n的最大整数
select rand() from dual;
select floor(10.1) from dual;
round(n,y)对n进行四舍五入,保留y位小数
select round(3.14159,3) from dual;
truncate(n,y)对n保留y位小数,不进行四舍五入
select truncate(3.14159,3) from dual;
rand()返回0-1之间的随机数
select rand() from dual;
3、日期和时间函数
now()返回当前日期时间
select now() from dual;
curdate()返回当前日期
select curdate() from dual;
curtime()返回当前时间
select day('2024-11-7') from dual;
select curtime() from dual;
year(date)返回某一个指定日期的年
select year('2024-11-7') from dual;
month(date)返回某一个指定日期的月
select month('2024-11-7') from dual;
day(date)返回某一个指定日期的日
select day('2024-11-7') from dual;
timestampdiff(interval,datetime1,datetime2)返回两个日期时间相隔的整数,单位由interval来定义
interval可取值:year,month,day,hour,minute,second
select timestampdiff(year,'2004-11-14','2024-11-7') from dual;
date_format(date,pattern)格式化日期
select date_format(now(),'%Y年%m月%d日,%H:%i:%s') from dual;
格式化参数:
%Y 表示四位数字的年份%m表示两位数字的月分
%d表示两位数字的日
%H表示两位数字的小时,24小时制 %h表示12小时制
%i表示两位数字的分钟
%s表示两位数字的秒数
4、流程控制函数
if(f,v1,v2)如果f为真,则返回v1,否则返回v2
select if(5>2,'yes','no') from dual;
ifnull(v1,v2)如果v1不为null,则返回v1,否则返回v2
select ifnull(null,'0') from dual;
case when f1 then v1 when f2 then v2,…… else v end如果f1为真,
则返回v1;如果f2为真,则返回v2;……,否则返回v
5、系统信息函数
datebase()返回当前操作的数据库
select database();
user()返回系统当前登陆用户
select user();
version()返回mysql服务器的版本
select version();