【MySQL】高级函数
| 高级函数 | 语法 | 例 |
|---|---|---|
| 数据类型转换 | cast(x as type) | 字符串转为日期 - cast("20210607" as date) - 2021-06-07 |
| 表达式是否成立 | if(expr,v1,v2) 如果表达式expr成立,则返回v1,否则返回v2 |
返回1>0的结果 - if(1>0,'yes','no') - yes |
| 返回第一个非空表达式 | coalesce(expr1,expr2,...,exprn) | - coalesce(null,null,"clay",null,"clay2") - clay |
| null替换 | ifnull(v1,v2) 如果v1值不为null,则返回v1,否则返回v2 |
- ifnull(null,"clay") - clay |
| 表达式是否为null | isnull(expr) 表达式是否为null,是则返回1,否则返回0 |
- isnull("clay") - 0 - isnull(null) - 1 |
| 字符串相等返回null | nullif(expr1,expr2) 字符串expr1与expr2相等,则返回null,否则返回expr1 |
- nullif("11","2") - 11 - nullif("a","a") - null |
| 返回最近生成的自增id(auto_increment) | last_insert_id() | - last_insert_id() - 2 |
| 表达式分支 | case expression when condition1 then result1 when condition2 then result2 ... when conditionN then resultN else redult end case表示函数开始,end表示函数结束。如果condition1成立,则返回result1;如果condition2成立,则返回result2;如果全部不成立,则返回result。当有一个成立,后面的则不执行。 |
- select name,(case when count<5 then '<5' when count >=5 and count<10 then '<10' else '>10' end) as level from ... - name level clay <10 alsi <10 tom <5 |

浙公网安备 33010602011771号