内置函数
1、日期内置函数
-
日期类型:DATE/DATETIME/DATESTAMP
datediff(date1, date2)==date1-date2;
-
插入日期,时间,时间戳
insert into tt values(current_data(), now(), current_timestamp());
-
查询在过去两分钟内的记录,根据时间戳来进行计算
select * from table where date_sub(now(), interval 2 minute) > now();
2、字符串内置函数
-
将emp表中的ename的除首字母外其余字母全部小写显示
select concat(substring(ename, 1, 1), lcase(substring(ename, 2))) from emp;
3、数学内置函数
-
查询每个部门的平均工资,使用 format(num, decimal_places) 描述平均工资
select deptno, format(avg(sal), 2) from emp group by deptno;
4、其它内置函数
-
查询当前用户--select user();
-
对一个字符串进行md5摘要,摘要后得到一个32位字符串--select md5(str);
-
显示当前正在使用的数据库--select database();
-
MySQL数据库使用该函数对用户加密--select passwd()
-
如果val1为null,返回val2,否则返回val1的值--select ifnull(var1, var2)
5、实例
-
查找字符串'10,A,B' 中逗号','出现的次数cnt
select length('10,A,B') - length(replace('10,A,B', ',', '')) as cnt;