> For the complete documentation index, see [llms.txt](https://zpliu.gitbook.io/booknote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zpliu.gitbook.io/booknote/mysql/chang-jian-han-shu/01-ji-ben-han-shu.md).

# 函数

## 1.概念

将一组逻辑封装在方法体之中，对外暴露方法名，隐藏了其中的细节，提高了代码的重用性

* `单行函数` 只有一个参数
* `分组函数` 参数为一组

## 2.单行函数

* 字符函数
* 数学函数
* 日期函数
* 流程控制函数
* 其他函数

### 2.1字符函数

`SHOW VARIABLES LIKE '%char%';`查看当前字符集

* length("dsad") 返回值为字节数目，中文字符在`utf-8`字符集中占3个字节
* `concat` 连接字符
* `upper`大写
* `lower`小写
* `substr`截取字符 包含4个重载函数
  * `substr(str,startPos)`从开始位置到字符结尾
  * substr(str,startPos,length)

函数嵌套调用

```sql
SELECT 
  CONCAT(UPPER(title), id) 
FROM
  `pmw_admanage`
```

* `instr(str,substr)` 返回子字符串第一次出现的索引，找不到则返回0
* `TRIM`去除字符串首位空格，也可以去掉指定字符;字符会区别大小写

  ```sql
  SELECT 
    TRIM('a' FROM 'aaaaVaa') AS output ;
  ```
* `LPAD`字符左填充 `SELECT LPAD('aa',10,'**') ;`将会返回一个10个字符的字符串
  * 如果指定填充后的长度小于原始字符串，则对原始字符串进行截断
* `RPAD`字符进行右填充
* `REPLACE` 默认全部替换`SELECT REPLACE('asdsad','dsad','d');`

### 2.2数学函数

* `ROUND`进行四舍五入,第二个参数保留的小数位数
* `CEIL`向上取整，返回>=该参数的最小整数
* `floor`向下取整，返回<=该小数的最大整数
* `truncate`对小数进行截断
* `mod` 取余，被除数为正则为正

### 2.3日期函数

* `now()`返回日期加时间
* `CURTIME()`返回当前时间
* `CURDATE()`返回当前日期
* `year()` 获取日期中的年 `SELECT YEAR('2020-02-01');`
* `MONTHNAME(NOW());`获取月份的名字或者数字`month()`
* `SELECT    STR_TO_DATE('2020/02/1','%Y/%m/%d');`将用户输入的字符串解析成mysql中的日期数据
* `SELECT DATE_FORMAT('2020-02-27',"%Y年%m月%d日");`将日期解析成对应的字符串

### 2.4其他函数

* `version()`
* `database`
* `user`

### 2.5流程控制函数

* 类似三元运算符

  `SELECT IF(id>10,'有','无') FROM pmw_goodstype;` IF对id进行判断为真返回有
* case函数

  * 1.对字段进行处理

  当id等于5的时候，对id进行加倍，否则按照原来的来显示

  ```sql
  case 要判断的语句或表达式
    when 常量 then 执行的操作;
    when 常量 then 执行操作;
    else 默认值语句;
    end

    SELECT id,
  CASE id
  WHEN 5 THEN id*2 #这里没有分号
  ELSE id
  END
  FROM `pmw_goodstype`;
  ```

  * 2.进行多重判断，执行对应的语句

    对id字段进行判断

    ```sql
    SELECT id,
    CASE 
    WHEN id>5 THEN 'A'
    WHEN  id<=4 THEN 'B'
    ELSE 'c'
    END AS Grade
    FROM `pmw_goodstype`;
    ```

## 3.分组函数

给函数传递一组值，最后得到一个返回值

* `sum`

  `SELECT SUM(id) FROM pmw_goodstype;` 对id字段求和;

  如果sum函数中放的为字符型，则会将字符型转化为数字；转换失败则会返回0；不支持`NULL`类型，自动忽略
* `avg`

  忽略`NULL`值
* `max`
  * 支持字符型和日期型数据
* `min`
* `count`
  * 支持的数据类型更多了，计算非`NULL`类型的数目
  * `count(*)`统计行数，\*代表所有字段
  * count(1) 统计行数 1表示新增一列，计算这一列的数目

### 3.1 简单的嵌套使用

* 去重之后再进行求和

```sql
SELECT SUM(DISTINCT id),SUM(id) FROM `pmw_goodstype`;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://zpliu.gitbook.io/booknote/mysql/chang-jian-han-shu/01-ji-ben-han-shu.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.
