# 第七章 函数

本节为您提供最常用的MySQL函数，包括聚合函数，字符串函数，日期时间函数，控制流函数等。

## MySQL聚合函数

* [MySQL聚合函数](http://www.yiibai.com/mysql/aggregate-functions.html) - 提供最常用的MySQL聚合函数的简要概述。
* [avg()函数](http://www.yiibai.com/mysql/avg.html) - 计算一组值或表达式的平均值。
* [count()函数](http://www.yiibai.com/mysql/count.html) - 计算表中的行数。
* [instr()函数](http://www.yiibai.com/mysql/instr.html) - 返回子字符串在字符串中第一次出现的位置。
* [sum()函数](http://www.yiibai.com/mysql/sum.html) - 计算一组值或表达式的总和。
* [min()函数](http://www.yiibai.com/mysql/min.html) - 在一组值中找到最小值。
* [max()函数](http://www.yiibai.com/mysql/max-function.html) - 在一组值中找到最大值。
* [group\_concat()函数](http://www.yiibai.com/mysql/group_concat.html) - 将字符串从分组中连接成具有各种选项(如`DISTINCT`，`ORDER BY`和`SEPARATOR`)的字符串。
* [MySQL标准偏差函数](http://www.yiibai.com/mysql/standard-deviation.html) - 显示如何计算人口标准偏差和样本标准偏差。

## MySQL字符串函数

* [concat()函数](http://www.yiibai.com/mysql/sql-concat-in-mysql.html) - 将两个或多个字符串组合成一个字符串。
* [length()函数\&char\_length()函数](http://www.yiibai.com/mysql/string-length.html) - 以字节和字符获取字符串的长度。
* [left()函数](http://www.yiibai.com/mysql/left-function.html) - 获取指定长度的字符串的左边部分。
* [replace()函数](http://www.yiibai.com/mysql/string-replace-function.html) - 搜索并替换字符串中的子字符串。
* [substring()函数](http://www.yiibai.com/mysql/substring.html) - 从具有特定长度的位置开始提取一个子字符串。
* [trim()函数](http://www.yiibai.com/mysql/trim.html) - 从字符串中删除不需要的字符。
* [find\_in\_set()函数](http://www.yiibai.com/mysql/find_in_set.html) - 在逗号分隔的字符串列表中找到一个字符串。
* [format()函数](http://www.yiibai.com/mysql/format-function.html) - 格式化具有特定区域设置的数字，舍入到小数位数。

## MySQL控制流函数

* [case()函数](http://www.yiibai.com/mysql/case-function.html) - 如果满足`WHEN`分支中的条件，则返回`THEN`分支中的相应结果，否则返回`ELSE`分支中的结果。
* [if语句](http://www.yiibai.com/mysql/if-statement.html) - 根据给定的条件返回一个值。
* [ifnull()函数](http://www.yiibai.com/mysql/ifnull.html) - 如果第一个参数不为`NULL`，则返回第一个参数，否则返回第二个参数。
* [nullif()函数](http://www.yiibai.com/mysql/nullif.html) - 如果第一个参数等于第二个参数，则返回`NULL`，否则返回第一个参数。

## MySQL日期和时间函数

* [curdate()函数](http://www.yiibai.com/mysql/curdate.html) - 返回当前日期。
* [datediff()函数](http://www.yiibai.com/mysql/datediff.html) - 计算两个`DATE`值之间的天数。
* [day()函数](http://www.yiibai.com/mysql/day.html) - 获取指定日期月份的天(日)。
* [date\_add()函数](http://www.yiibai.com/mysql/date_add.html) - 将时间值添加到日期值。
* [date\_sub()函数](http://www.yiibai.com/mysql/date_sub.html) - 从日期值中减去时间值。
* [date\_format()函数](http://www.yiibai.com/mysql/date_format.html) - 根据指定的日期格式格式化日期值。
* [dayname()函数](http://www.yiibai.com/mysql/dayname.html) - 获取指定日期的工作日的名称。
* [dayofweek()函数](http://www.yiibai.com/mysql/dayofweek.html) - 返回日期的工作日索引。
* [extract()函数](http://www.yiibai.com/mysql/extract.html) - 提取日期的一部分。
* [now()函数](http://www.yiibai.com/mysql/now.html) - 返回当前日期和时间。
* [month()函数](http://www.yiibai.com/mysql/month.html) - 返回一个表示指定日期的月份的整数。
* [str\_to\_date()函数](http://www.yiibai.com/mysql/str_to_date.html) - 将字符串转换为基于指定格式的日期和时间值。
* [sysdate()函数](http://www.yiibai.com/mysql/sysdate.html) - 返回当前日期。
* [timediff()函数](http://www.yiibai.com/mysql/timediff.html) - 计算两个`TIME`或`DATETIME`值之间的差值。
* [timestampdiff()函数](http://www.yiibai.com/mysql/timestampdiff.html) - 计算两个`DATE`或`DATETIME`值之间的差值。
* [week()函数](http://www.yiibai.com/mysql/week.html) - 返回一个日期的星期数值。
* [weekday()函数](http://www.yiibai.com/mysql/weekday.html) - 返回一个日期表示为工作日/星期几的索引。
* [year()函数](http://www.yiibai.com/mysql/year.html) - 返回日期值的年份部分。

## MySQL比较函数

* [coalesce()函数](http://www.yiibai.com/mysql/coalesce.html) - 返回第一个非`NULL`参数，这非常适合用于将值替换为`NULL`。
* [greatest()函数\&least()函数](http://www.yiibai.com/mysql/greatest-least.html) – 使用`n`个参数，并分别返回`n`个参数的最大值和最小值。
* [isnull()函数](http://www.yiibai.com/mysql/isnull-function.html) - 如果参数为`NULL`，则返回`1`，否则返回`0`。

## 其他MySQL函数

* [last\_insert\_id()函数](http://www.yiibai.com/mysql/last_insert_id.html) - 获取最后插入的记录的最后生成的序列号。
* [cast()函数](http://www.yiibai.com/mysql/cast.html) - 将任何类型的值转换为具有指定类型的值。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang-book.gitbook.io/mysql/di-qi-zhang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
