SQL語言總結——常用函數

常見函數

  1. 格式:select 函數名(實參) 【from 表】

  2. 分類:

    • 單行函數:

      • 字符函數:

        a. length(“字符zifu”);//獲取字節個數,漢字UTF8三字節,GBK2字節,英文都是1字節

        b. concat(“str1”,“str2”);//拼接字符

        c. upper(""), lower("");//大小寫轉換

        d. substr(“str”,2,3);//截取從2往後3個長度字符,基1索引

        e. instr(“str”,“substr”);//返回子串初始索引

        f. trim(“aa” FROM “aaazaaazaa”);//總頭尾刪除子串,得到azaaaz

        g. lpad(“str1”,len,“str2”),rpad(“str1”,len,“str2”);//str1左右填充str2以達到len長度的字符串

      • 數學函數:

        a. round(-1.25),ceil(-1.25),floor(-1.25);//四捨五入-1,向上取整-1,向下取整-2

        b. truncate(2.23,1);//截斷函數,小數後保留幾位,2.2

        c. mod(a,b);//取模

      • 日期函數:

        ​ a. now();//返回當前時間

        ​ b. curdate();//當前日期

        ​ c. year(now()), year(“1996-08-12”), month(), monthname(), day(), hour();//返回具體時間

        ​ d. str_to_date(“1995-02-01”,"%y-%m-%d");//按固定格式解析字符串到時間

      • 流程控制函數:

        ​ a. if(cond, stat1,stat2);//條件中真執行stat1,否則stat2

      • 其他函數:

        ​ a. version();//數據庫版本

        ​ b. database();//當前數據庫

        ​ c. user();//當前用戶

    • 統計函數:

      • 求和函數:sum(),sum(distinct id)
      • 求平均:avg()
      • 最大值:max()
      • 最小值:min()
      • 計算個數:count()
      • 分組查詢:select Max(salary) from table_name group by job_id;//以某字段分組
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章