關于mysql內置函數

字符串函數

查看字符的ascii碼值ascii(str),str是空串時返回0

    select ascii('a');

查看ascii碼值對應的字符char(數字)

  select char(97);

拼接字符串concat(str1,str2...)

  select concat(12,34,'ab');

包含字符個數length(str)

    select length('abc');

截取字符串

    * left(str,len)返回字符串str的左端len個字符
    * right(str,len)返回字符串str的右端len個字符
    * substring(str,pos,len)返回字符串str的位置pos起len個字符

   select substring('abc123',2,3);

去除空格

     ltrim(str)返回刪除了左空格的字符串str
     rtrim(str)返回刪除了右空格的字符串str
     trim([方向 remstr from str)返回從某側刪除remstr后的字符串str,方向詞包括both、leading、trailing,表示兩側、左、右
     select trim('  bar   ');
     select trim(leading 'x' FROM 'xxxbarxxx');
     select trim(both 'x' FROM 'xxxbarxxx');
     select trim(trailing 'x' FROM 'xxxbarxxx');
     返回由n個空格字符組成的一個字符串space(n)
     select space(10);
     替換字符串replace(str,from_str,to_str)
     select replace('abc123','123','def');

大小寫轉換,函數如下

    *lower(str)
    *upper(str)
    *select lower('aBcD');

數學函數

求絕對值abs(n)

    select abs(-32);

求m除以n的余數mod(m,n),同運算符%

    select mod(10,3);
    select 10%3;

地板floor(n),表示不大于n的最大整數

    select floor(2.3);

天花板ceiling(n),表示不小于n的最大整數

    select ceiling(2.3);

求四舍五入值round(n,d),n表示原數,d表示小數位置,默認為0

    select round(1.6);

求x的y次冪pow(x,y)

    select pow(2,3);

獲取圓周率PI()

    select PI();

隨機數rand(),值為0-1.0的浮點數

    select rand();

還有其它很多三角函數,使用時可以查詢文檔

日期時間函數

獲取子值,語法如下

    * year(date)返回date的年份(范圍在1000到9999)
    * month(date)返回date中的月份數值
    * day(date)返回date中的日期數值
    * hour(time)返回time的小時數(范圍是0到23)
    * minute(time)返回time的分鐘數(范圍是0到59)
    * second(time)返回time的秒數(范圍是0到59)
    * select year('2016-12-21');

日期計算,使用+-運算符,數字后面的關鍵字為

   year、month、day、hour、minute、second   
   select '2016-12-21'+interval 1 day;

日期格式化date_format(date,format),format參數可用的值如下

 *獲取年%Y,返回4位的整數
 *獲取年%y,返回2位的整數
 *獲取月%m,值為1-12的整數
 *獲取日%d,返回整數
 *獲取時%H,值為0-23的整數
 *獲取時%h,值為1-12的整數
 *獲取分%i,值為0-59的整數
 *獲取秒%s,值為0-59的整數
  select date_format('2016-12-21','%Y %m %d');

當前日期current_date()

  select current_date();

當前時間current_time()

  select current_time();

當前日期時間now()

  select now();select ascii('a');
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容