7.mysql 基础之常用函数

发布时间:2026/7/29 17:18:56

7.mysql 基础之常用函数 一单行函数1. concat(str1,str2,str3) #字符串拼接函数2.length列名#查看字段长度3.curdate() 当前日期curtime() 当前时间now()当前时间sysdate()当前时间4.truncate(123.2344,0) 结果为123 #截取数字后面数为截取保留的小数位round123.5672结果为123.57 四舍五入5.datediff(day1,day2) #两个日期的相差天数6.date_format(day,ft) #日期转换为字符串格式,ft为格式例如 %Y-%m-%d7.str_to_date(str,ft)# 字符串转换为日期,ft为格式例如 %Y-%m-%d8.ifnullvalue,defalut#如果value值为null显示为defalut不为null则为value例子1. select concat(hello,nihao,pengyou) from dual结果hellonihaopengyou2.select length(hello) from dual结果53.select curdate() ,curtime() ,now(),sysdate() from dual结果2022-04-05 11:44:58 2022-04-05 11:44:58 2022-04-05 11:44:584.select truncate(123.456,1) ,round(123.456,1) from dual结果123.4 123.55.select datediff(now(), 2000-01-01) from dual结果81306.select date_format(now(),%Y:%m:%d) from dual结果2022:04:057.select str_to_date (2021-01-01,%Y-%m-%d) from dual结果2021-01-018.select ifnull(null,0),ifnull(2,0) from dual结果0 2二多行函数聚合函数1.常用聚合函数avg、sum、max、min、count2.查询所有一般使用count(*)或者count1一般不使用count字段因为字段中值有可能为null3.聚合函数一般和group by 一块使用4.如果过滤条件中包含聚合函数 则使用having关键字5.如果过滤条件中不包含聚合函数 则使用where 关键字性能高6. group by 一般放在 from 和where 之后order by 和limit 关键字之前having 放在group by之后7. 使用group by 关键字select 字段除了聚合函数其他字段必须为group by后的字段例如select e.department_id ,max(salary),avg(salary),min(salary)from emploees e join department don e.department_id d.department_idwhere d.department_id in (20,30,40,50,60)group by emploees_idhaving min(salary)5000

相关新闻