MySQL常用的函数

xiaojiuaigc@163.com 发布于 2025-04-13 97 次阅读


      • 聚合函数:min() max() avg() count() sum()
      • 其他:abs(),mod(),ceil() …..
    • 日期函数:now() sysdate() date_format()….
    • 字符串函数:concat() substr() upper() lower()…..
    • 条件控制函数:case when 、if、ifnull….

case wen语法

语法1

case degree
            when 1 then '初中'
            when 2 then '高中'
            when 3 then '大专'
            when 4 then '本科'
            when 5 then '硕士'
            when 6 then '博士'
            else '其他'
    end

 

case degree
            when 1 then '初中'
            when 2 then '高中'
            when 3 then '大专'
            when 4 then '本科'
            when 5 then '硕士'
            when 6 then '博士'
            else '其他'
    end

语法2

case expr when val1 then res1 [ when val2 then res2 ] else res end ;仅限于等值匹配

if函数语法

if(条件, 条件为true取值, 条件为false取值)
  • 场景
    • 字段有两种值,需要转为前端需要显示的值
    • 比如
      • 性别字段值为1或2,前端要求展示男或女

ifnull函数语法

ifnull(expr, val1)    如果expr不为null,取自身,否则取val1
  • 场景
    • 字段为空,需要转为前端需要显示的值
    • 比如
      • 城市字段值为null,前端需要显示未知
    • 加密函数:md5() aes() …..