MySQL中的数据类型和函数
数据类型
整型
浮点型和定点型
时间日期类型
字符型
函数
函数太多,示例就不给大家一一例举了,所用格式都一样。
数值类型函数
- abs(x):返回x的绝对值
select abs(-15) as 绝对值;
- ceil(x):向上取整
select ceil(3.14) as 向上取整;
- floor(x):向下取整
select floor(3.14) as 向下取整;
- round(x,y=0):四舍五入,将x四舍五入y位小数,y不传返回整数,y为负数时,保留x值到小数点左边y位
- truncate(x,y):截断函数,返回被舍去至小数点后y位的数字x,y为负数时截断小数点左边
- mod(x,y):返回x除以y的余数
- rand():生成随机数
字符函数
- concat():字符串连接,如果有一个为null,则返回null
select concat('hello', ' ', 'world') as 连接字符串;
# 结果为hello worldselect concat('hello', null, 'world') as 连接字符串;
# 结果为null
- concat_ws(x,s1,s2):指定分隔符的字符连接函数,x是连接分隔符,如果分隔符是null,则结果为null
select concat_ws('-', '2023', '05', '17') as 拼接日期;
#结果2023-05-17select concat_ws(null, 'a', 'b') as 拼接结果;
#结果 null
- lower(str):大写转小写
select lower('HELLO WORLD') as 小写;
- upper(str):小写转大写
select upper('hello world') as 大写;
- length(str):字符串长度
- ltrim(str):删除字符串左侧空格
- rtrim(str):删除字符串右侧空格
- trim(str):删除字符串两侧空格
- substr(str,n,len):截取字符串,字符串str从n的位置截取长度为len的字符串,如果n为负数,从字符串结尾处截取
- left(str,n):返回字符串左边n个字符
- right(str,n):返回字符串右边n个字符
- replace(str,from_str,to_str):替换函数,字符串str中所有的字符串from_str均被to_str 替换,然后返回这个字符串
- fromat(x,n):将数字x格式化,并以四舍五入保留n位小数,结果以字符串的形式返回,若n为0,则返回值结果不含小数部分
日期时间类型
- curdate()/current_date():获取当前日期,YYYY-MM-DD格式
select curdate() as 当前日期;
-- 或者
select current_date() as 当前日期;
#例如:2025-05-17
- curtime()/current_time():获取当前时间,HH:MM:SS格式
select curtime() as 当前时间;
-- 或者
select current_time() as 当前时间;
#例如:14:30:45
- week(date):返回date为一年中的几周
select week('2025-05-17') as 周数;
# 返回这天在这年中是第几周
- now()/sysdate():获取当前时间和日期,YYYY-MM-DD HH:MM:SS格式
select now() as 当前日期时间;
-- 或者
select sysdate() as 当前日期时间;
- date_add(date,interval expr type):执行日期的加运算,date是一个datetime或date值,指定起始时间。expr是时间间隔,type为关键字,如YEAR,MONTH,DAY,WEEK,HOUR等
- datediff(date1,date2):计算两个日期之间的间隔天数
- unix_timestamp(date):返回date的UNIX时间戳
- form_unixtime(unix): 返回unix时间戳的日期戳
- date_format(date,format): 日期格式化,按format格式化date值
- str_to_date(date,format): 将字符串转换成date类型
聚合函数(常用)
- avg():返回平均值
select avg(salary) as 平均薪水 from emp; #as是将原名转换成别名所用的前缀
- sum():返回值的和
select sum(salary) as 薪水水平 from emp;
- count():返回某列的行数
select count(*) as 员工总数 from emp;
- max():返回最大值
select max(salary) as 最高薪水 from emp;
- min():返回最小值
select min(salary) as 最低薪水 from emp;
- 注意:
- 聚合函数会自动忽略空值,不需要手动增加条件排除NULL;
- 聚合函数不能作为where子句后的限制条件;
流程函数
- if(value,t,f):如果value为真返回t,否则返回f
select ename,salary,if(salary>3000,'高新','普通') as 薪资等级
from emp;
如果salary > 3000为真(即工资高于3000),返回"高薪";
否则,返回"普通"。
- ifnull(column,value):如果column为空(null)返回value,否则返回column
select ename,ifnull(commission,0) as 提成 #如果commission为null,则返回0
from emp;
如果commission为空,显示为0;
如果不为空,显示实际的提成数值。
- 在sql语句中有null参加运算,结果都为null(建议使用ifnull空值处理函数预先处理)
select salary + commission as 总收入 from emp;
如果某个员工的commission为空(NULL),那么这个员工的总收入结果也会是NULL,即使其他部分有具体数字。
- 为了避免NULL导致结果也变成NULL,我们可以在运算前,先将空值转换成一个默认值。例如,把NULL看作0:
select salary + ifnull(commission,0) as 总收入 from emp;
这样如果commission是空,实际上就用0代替,保证运算结果正确、合理。
自定义函数
- function,可以使用自定义函数来扩展数据库的功能
#创建函数
create function 函数名([参数1 数据类型[,参数2 数据类型。。。]])
returns 返回值类型
begin
函数逻辑代码
end;
#调用函数
select 函数名([参数1,参数2.。。]);
#删除函数
drop function [if exists] 函数名;
例:
- 创建一个函数 my_avg,输入两个数字,返回它们的平均值。
# 创建函数
create function my_avg(a decimal(10,2),b decimal(10,2))
returns decimal(10,2)
beginreturn (a+b)/2;
end;# 调用函数
select my_avg(80,90) as 平均值;# 删除函数
drop function if exists my_avg;