count()计数
日常业务中不仅需要将表连接起来,还经常会用到数据的统计。
count()方法是统计数据的个数。
例如:我们想知道有多少条记录就可以使用:
select
count(student_id) as num
from student_info
但是有的表可能会出现一张表有重复的数据,例如我们上一章讲的使用union all 方法连接数据,直接count会将重复值给计算上,如果我们想看有多少个学生就可以使用
select
count(distinct student_id) as stu_num
from student_info
sum()求和:
select
sum(student_id) as sum
from student_info
对某一些数值进行求和统计
max() 和min() 最大最小值:
例如:我们想知道目前最大的学生年龄和最小的学生年龄分别是多少就可以用:
select
MAX(distinct age) as max_age,
MIN(distinct age) as min_age
from student_info
length()数据的长度:
查看数据字段的长度
select
lenght(student_id) as leng
from student_info
year() month() day() hour():
日期转化:要处理的数据格式为'yyyy-mm-dd hh:mm:ss'
计算数据'2023-01-01 12:01:00'
year():取数据的年 year('2023-01-01 12:01:00') ----2023
month():取数据的月份 month('2023-01-01 12:01:00') ----01
day():取数据的日 day('2023-01-01 12:01:00') ----01
hour():取数据的小时 hour('2023-01-01 12:01:00') ----12
date():
转化为年月日的形式:
date('2023-01-01 12:01:00')----2023-01-01
或者使用字符串截断的形式:
substr('2023-01-01 12:01:00',1,10) -----2023-01-01 代表截取第一个字符到第10个字符的内容