
MySQL数学函数

ABS(x):返回x的绝对值
SQL语句:
select ABS(-2) from dual;

ceil(x):返回不小于x的最小整数值 大于等于
SQL语句:
select ceil(-2.1) from dual;
select ceil(2.3) from dual;

floor(x):返回不大于x的最大整数值 小于等于
SQL语句:
select floor(2.1) from dual;

mod(x,y):返回x/y的模(取余)
SQL语句:
select mod(5,3) from dual;

rand():返回0~1内的随机值
SQL语句:
select rand() from dual;

round(x,y):返回参数x的四舍五入的有y位小数的值
SQL语句:
select round(rand(),2) from dual;

truncate(x,y):返回数字x截断位y位小数的结果
SQL语句:
select truncate(1.598,2) from dual;

MySQL聚合函数

测试表数据

avg():返回某列的平均值
SQL语句:
select round(avg(prod_price),2) from products;

count():返回某列的行数
SQL语句:
select count(*) from products;

max():返回某列的最大值
SQL语句:
select max(prod_price) from products;

min():返回某列的最小值
SQL语句:
select min(prod_price) from products;

sum():返回某列值之和
SQL语句:
select sum(prod_price) from products;

MySQL字符串函数

concat(s1,s2,….):字符串连接
SQL语句:
select concat('I','come','from','china') from dual;

left(str,x):返回字符串str最左边的x个字符
SQL语句:
select left('123456',3) from dual;

lpad(str,n,pad):在str最左边填充n个pad
SQL语句:
select lpad('wwww',6,'0') from dual;

replace(str,s1,s2):用字符串s2替换字符str中所有出现的字符串s1
SQL语句:
select replace('12344455','4','0') from dual;

substring(str,x,y):返回从字符串str的x位置起y个字符长度的字串
SQL语句:
select substring('123456','2',5) from dual;

MySQL日期时间函数


now():返回当前的日期和时间
SQL语句:
select now() from dual;

year(date):返回日期date的年份
SQL语句:
select year('2021-04-12') from dual;

date_format(date,fmt):返回按字符串fmt格式日期date值(MySQL下)
to_char(date,fmt): 返回按字符串fmt格式日期date值(Oracle下)
SQL语句:
select date_format(now(),'%Y-%m-%d') from dual;

SELECT to_char(sysdate,'YYYY-MM-DD') FROM dual;

date_add(date,interval expr type):返回一个日期或时间值加上一个时间间隔的时间值
SQL语句:
select date_add('2021-04-10',interval 1 day) from dual;
select date_add('2021-04-10',interval -1 day) from dual;

datediff(expr,expr2):返回起始时间expr和结束时间expr2之间的天数
SQL语句:
select datediff('2021-04-11','2021-04-13') from dual;

MySQL流程函数

ifnull(value1,value2):如果value1不为空,返回value1,否则返回value2
SQL语句:
select ifnull(id,2) from stu_score;

case when [value1] then[result1]….else[default]end:如果value1是真,返回result1,否则返回result
SQL语句:
select
id,name,score,
case
when score >= 90 then '优秀'
when score >=80 then '良好'
when score >=60 then '及格'
else '不及格'
end as '等级'
from stu_score;

Oracle函数差异

本文详细介绍了MySQL中的数学函数如ABS、CEIL、FLOOR等,聚合函数如AVG、COUNT、MAX、MIN、SUM,字符串函数如CONCAT、LEFT、REPLACE等,以及日期时间函数NOW、YEAR、DATE_FORMAT等。同时,对比了Oracle中的相关函数差异,帮助读者深入理解数据库操作。
1970

被折叠的 条评论
为什么被折叠?



