cast 的使用
cast 函数是将数据类型进行转换, 比如 int 转 string. 基础用法是 CAST(expr AS type [ARRAY])
- expr: 表达式,一般是某个字段
- type: 需要转换成的类型
比如表中有 id 字段为 int 类型,
cast(id as char), 就是将 int 类型的 id 转换成 char
cast 的限制
在使用 cast 转换类型时需要注意一点, cast能支持的类型不多, 我们常用的 varchar2 之类的类型是不支持的. 支持的类型有
- BINARY[(N)]
- CHAR[(N)]
- DATE
- DATETIME
- DECIMAL[(M[,D])]
- SIGNED
- [INTEGER]
- TIME
- UNSIGNED [INTEGER]
参考
https://stackoverflow.com/questions/15368753/cast-int-to-varchar
https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_cast