聚合函数

**sum**:求和
**avg**:平均值
**max**:最大值
**min**:最小值

  1. 1 | select sum(studentname) as 总和 from student;
  2. 2 | select avg(studentname) as 平均数 from student;
  3. 3 | select max(studentname) as 最高 from student;
  4. 4 | select min(studentname) as 最低 from student;

count():统计数据

  • **count(字段)** :会忽略所有的null值
  • **count(*)**:不会忽略null值,本质: **计算行数**
  • **chout(1)**:不会忽略null值,本质:**计算行数** ``sql 1 | select count(borndate`) from student; —count(字段),会忽略所有的null值

2 | select count() from student; —count(), 不会忽略null值,本质 计算行数

3 |select count(1) from student; —chout(1), 不会忽略null值,本质,计算行数

  1. <a name="CXO47"></a>
  2. ## 常用函数
  3. <a name="yFHPl"></a>
  4. ### 函数的功能
  5. 封装了特定的一些功能,直接拿过来使用,可以实现对应的功能
  6. <a name="e4vys"></a>
  7. ###
  8. <a name="LT7Hd"></a>
  9. ### 函数的作用
  10. 为了提高`**select**`的能力
  11. > **注意**:函数没有改变自身的值,而是在真实的数据上面进行加工处理,展示新的结构
  12. <a name="cqDH7"></a>
  13. ###
  14. <a name="uaQUG"></a>
  15. ### 单行函数
  16. 单行函数分为五种类型:`字符函数`、`数值函数`、`日期函数`、`转换函数`、`通用函数`
  17. ```java
  18. --大小写控制函数
  19. selec empno,lower(name),upper(name),sal from emp; lower:小写,upper:大写

多行函数

多行函数是对一组数据进行运算,针对这一组数据值返回一个结果,也称为分组函数

  1. SELECT max(sal),min(sal),count(sal),sum(sal),avg(sal) FROM emp;

注:除了maxmincountsumavg其他都是单行函数

字符串函数

  1. 1 | selct ename ,length(ename,2,3) from emp
  2. substring字符串截取,2:从字符下标为2开始:3:截取长度3 (下标从1开始)
  3. 2 | select char_length('哈哈') --字符串长度,//2
  4. --查询姓周的,并且改成姓在的
  5. 3 | select replace(studentname,'周','在') from student where studentname like '周%'
  6. --大小写控制函数
  7. 4 | selec empno,lower(name),upper(name),sal from emp; lower:小写,upper:大写

数值函数

  1. 1 | select abs(5) , ceil(6.4),floor(5.4),round(3.6) from dual --dual 实际就是一个伪表
  2. abs(5) AS 绝对值 , // 5
  3. ceil(6.4) 向上取整 , // 7
  4. floor(5.4) 向下取整, // 5
  5. round(3.6) 四舍五入; --如果没有where条件的话,from dual可以省略不写
  6. 2 | select 10/3,10%3,mod(10,3)
  7. 3 | select rand() --返回一个01的随机数
  8. 4 | select sign() -- 判断一个数的符号,0-0,负数是-1,正数是1

日期和时间函数

  1. --curdate()年月日,curtime()时分秒
  2. --date()年月日, datetime()年月日时分秒
  3. select curdate(),curtime();
  4. select now(),sysdate(),sleep(3),now(),sysdate() from dual --now(),sysdate()年月日时分秒
  5. now()当前时间的年月时分秒
  6. insert into t_student values (555,,"zhaoli","男",now(),223,null,45)
  7. now()可以表示现在年月日时分秒,但是插入数据的时候还是要参照表的结构