功能:用作统计使用,又称为聚合函数或统计函数或组函数
分类:sum 求和、 avg 平均值、max 最大值、min 最小值、count 计数个数
一般和group by后面的字段搭配
可以和distinct(去重)搭配

  1. select
  2. COUNT(distinct salary),count(salary)
  3. from
  4. employees;

image.png

count函数

  1. select
  2. COUNT(distinct salary),count(salary)
  3. from
  4. employees;
  5. select
  6. COUNT(*) //计算主键不为null值的行数
  7. from
  8. employees;
  9. select
  10. COUNT(1)
  11. from
  12. employees;

效率:
MYISAM存储引擎下COUNT()的效率高
INNODB存储引擎下COUNT(
)和COUNT(1)效率差不多,比COUNT(字段)高一些

DATEDIFF()求两个日期相差的天数

  1. select
  2. DATEDIFF(now(),'1999-04-07')

image.png