一、數學函數:
#ABS 絕對值函數 select abs(-5) ; #BIN 返回二進制,OCT()八進制,hex十六進制 select bin(2); #ceiling 天花板整數,也就是大於x的整數 select CEILING(-13.5); #EXP(X)自然對數e為底的x次方 select EXP(2); #FLOOR(X)小於x的最大整數 select FLOOR(3.5); #返回集合中最大的值,least返回最小的,注意跟max,min不一樣,max里面跟的是col,返回這個列的最大值最小值 select GREATEST(1,2,3,4); #ln(x)返回對數 select LN(10); #log(x,y),以y為底x的對數 select log(4,2); #mod(x,y)返回x/y的余數 select mod(5,2); #pi() 返回pi select pi(); #RAND()返回0,1之間隨機數 select rand(); #round(x,y)返回x四舍五入有y位小數的值 select round(pi(),3); #sign(x) 返回x符號 select sign(-5); #sqrt(x)返回x的平方根 select sqrt(9); #truncate(x,y) 返回數字x截斷為y位小數的結果,就是不考慮四舍五入,直接砍掉 select TRUNCATE(pi(),4),ROUND(pi(),4),pi();
二、聚合函數(常用語group by從句select語句中)
#avg(col)返回指定列平均值 select AVG( quantity) from orderitems; #count(col)返回指定列中非null數量 select count(quantity) from orderitems; #min(col)max(col)返回指定列最大最小值 select max(quantity),min(quantity) from orderitems; #sum(col)返回制定列求和值 select sum(quantity) from orderitems; #group_concat(col)返回這個列連接組合的結果,中間有逗號隔開 select group_concat(prod_id) from orderitems
三、字符串函數
#ASCII(str)返回字符串的ascii碼值 select ASCII('12'); #bit_length(str)返回字符串的比特長度 select bit_length('123'); #concat()連接字符串 select concat('1','我','2'); #concat_ws(sep,s1,s2,s3)連接字符串用sep隔開 select concat_ws('|','1','我','2'); #INSERT(str,pos,len,newstr),將字符串str從pos位置開始的len長度替換為newstr select 'hello world',INSERT('hello world',2,3,'傑哥哥'); #FIND_IN_SET(str,strlist)分析逗號分隔的list,如果發現str返回list中的位置 select find_in_set('abc','aabc,sdf,det,abc') #返回4 #LCASE(str),LOWER(str)都是返回小寫的結果,UPPER(),UCASE() 返回的都是大寫結果 select LCASE('UUSU'); #`LEFT`(str,len)從左到右從str中選擇長度為len的字符串,`RIGHT`(str,len)相反 select left('hello world',4); #length(str)返回str字符串長度 select length('hello world'); #trim(),ltrim()rtrim()分別去掉兩頭的空格,左邊的空格,右邊的空格 select trim(' hello '); #POSITION(substr IN str),返回substr首次在str中長線的位置 select POSITION('llo' IN 'hello world'); #QUOTE(str)用反斜杠轉義str中的單引號 select QUOTE("hello ' world") #`REPLACE`(str,from_str,to_str)在str中,把from str 轉成to_str select replace('hello world','hello','hi'); #repeat(str,count)重復str count次 select repeat('hello world',3); #reverse(str)顛倒str select reverse('hello world'); #STRCMP(expr1,expr2)比較兩個字符串,一模一樣返回0,不一樣返回1 select STRCMP('hello','world')
四、日期和時間函數
#curdate()或者current_date() NOW()是返回日期+時間 select current_date(); #curtime()當前時間,或者current_time() select curtime(); #DATE_ADD(date,INTERVAL expr unit)返回date加上int日期后的日期,date_sub()也是一樣的 select date_add(curdate(),interval 5 day); select date_add(curtime(),interval 5 hour); #DATE_FORMAT(date,format)按照格式轉化日期,具體的format格式可以查查 select date_format(CURDATE(),'%m-%d-%Y'); #dayofweek()DAYOFMONTH(date)DAYOFYEAR(date)返回日期中是一周中第幾天,一個月中第幾天,一年中第幾天 select DAYOFMONTH(CURDATE()); #dayname返回日期星期名 select dayname(CURDATE()); #hour(time),minute(time),month(date) select curtime(),hour(CURTIME()),minute(curtime()); select curdate(),month(curdate()),year(curdate()),day(curdate()); #MONTHNAME(date) 返回月份名稱 select monthname(curdate()); #quarter(date)返回日期的第幾季度 select quarter(curdate()); #week(date) select week(curdate()); #TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)計算兩個時間差,還有datediff() select TIMESTAMPDIFF(year,19920202,CURDATE())
參考博客:https://blog.csdn.net/sugang_ximi/article/details/6664748