一、數學函數 abs(x) 返回x的絕對值 bin(x) 返回x的二進制(oct返回八進制,hex返回十六進制) ceiling(x) 返回大於x的最小整數值 exp(x) 返回值e(自然對數的底)的x次方 floor(x) 返回小於x的最大整數值 greatest(x1,x2,...,xn)返回集合中最大的值 least(x1,x2,...,xn) 返回集合中最小的值 ln(x) 返回x的自然對數 log(x,y)返回x的以y為底的對數 mod(x,y) 返回x/y的模(余數) pi()返回pi的值(圓周率) rand()返回0到1內的隨機值,可以通過提供一個參數(種子)使rand()隨機數生成器生成一個指定的值。 round(x,y)返回參數x的四舍五入的有y位小數的值 sign(x) 返回代表數字x的符號的值 sqrt(x) 返回一個數的平方根 truncate(x,y) 返回數字x截短為y位小數的結果 二、聚合函數(常用於group by從句的select查詢中) avg(col)返回指定列的平均值 count(col)返回指定列中非null值的個數 min(col)返回指定列的最小值 max(col)返回指定列的最大值 sum(col)返回指定列的所有值之和 group_concat(col) 返回由屬於一組的列值連接組合而成的結果 三、字符串函數 ascii(char)返回字符的ascii碼值 bit_length(str)返回字符串的比特長度 concat(s1,s2...,sn)將s1,s2...,sn連接成字符串 concat_ws(sep,s1,s2...,sn)將s1,s2...,sn連接成字符串,並用sep字符間隔 insert(str,x,y,instr) 將字符串str從第x位置開始,y個字符長的子串替換為字符串instr,返回結果 find_in_set(str,list)分析逗號分隔的list列表,如果發現str,返回str在list中的位置 lcase(str)或lower(str) 返回將字符串str中所有字符改變為小寫后的結果 left(str,x)返回字符串str中最左邊的x個字符 length(s)返回字符串str中的字符數 ltrim(str) 從字符串str中切掉開頭的空格 position(substr in str) 返回子串substr在字符串str中第一次出現的位置 quote(str) 用反斜杠轉義str中的單引號 repeat(str,srchstr,rplcstr)返回字符串str重復x次的結果 reverse(str) 返回顛倒字符串str的結果 right(str,x) 返回字符串str中最右邊的x個字符 rtrim(str) 返回字符串str尾部的空格 strcmp(s1,s2)比較字符串s1和s2 trim(str)去除字符串首部和尾部的所有空格 ucase(str)或upper(str) 返回將字符串str中所有字符轉變為大寫后的結果 四、日期和時間函數 curdate()或current_date() 返回當前的日期 curtime()或current_time() 返回當前的時間 date_add(date,interval int keyword)返回日期date加上間隔時間int的結果(int必須按照關鍵字進行格式化),如:selectdate_add(current_date,interval 6 month); date_format(date,fmt) 依照指定的fmt格式格式化日期date值 date_sub(date,interval int keyword)返回日期date加上間隔時間int的結果(int必須按照關鍵字進行格式化),如:selectdate_sub(current_date,interval 6 month); dayofweek(date) 返回date所代表的一星期中的第幾天(1~7) dayofmonth(date) 返回date是一個月的第幾天(1~31) dayofyear(date) 返回date是一年的第幾天(1~366) dayname(date) 返回date的星期名,如:select dayname(current_date); from_unixtime(ts,fmt) 根據指定的fmt格式,格式化unix時間戳ts hour(time) 返回time的小時值(0~23) minute(time) 返回time的分鍾值(0~59) month(date) 返回date的月份值(1~12) monthname(date) 返回date的月份名,如:select monthname(current_date); now() 返回當前的日期和時間 quarter(date) 返回date在一年中的季度(1~4),如select quarter(current_date); week(date) 返回日期date為一年中第幾周(0~53) year(date) 返回日期date的年份(1000~9999) 一些示例: 獲取當前系統時間:select from_unixtime(unix_timestamp()); select extract(year_month from current_date); select extract(day_second from current_date); select extract(hour_minute from current_date); 返回兩個日期值之間的差值(月數):select period_diff(200302,199802); 在mysql中計算年齡: select date_format(from_days(to_days(now())-to_days(birthday)),'%y')+0 as age from employee; 這樣,如果brithday是未來的年月日的話,計算結果為0。 下面的sql語句計算員工的絕對年齡,即當birthday是未來的日期時,將得到負值。 select date_format(now(), '%y') - date_format(birthday, '%y') -(date_format(now(), '00-%m-%d') <date_format(birthday, '00-%m-%d')) as age from employee 五、加密函數 aes_encrypt(str,key) 返回用密鑰key對字符串str利用高級加密標准算法加密后的結果,調用aes_encrypt的結果是一個二進制字符串,以blob類型存儲 aes_decrypt(str,key) 返回用密鑰key對字符串str利用高級加密標准算法解密后的結果 decode(str,key) 使用key作為密鑰解密加密字符串str encrypt(str,salt) 使用unixcrypt()函數,用關鍵詞salt(一個可以惟一確定口令的字符串,就像鑰匙一樣)加密字符串str encode(str,key) 使用key作為密鑰加密字符串str,調用encode()的結果是一個二進制字符串,它以blob類型存儲 md5() 計算字符串str的md5校驗和 password(str) 返回字符串str的加密版本,這個加密過程是不可逆轉的,和unix密碼加密過程使用不同的算法。 sha() 計算字符串str的安全散列算法(sha)校驗和 示例: select encrypt('root','salt'); select encode('xufeng','key'); select decode(encode('xufeng','key'),'key');#加解密放在一起 select aes_encrypt('root','key'); select aes_decrypt(aes_encrypt('root','key'),'key'); select md5('123456'); select sha('123456'); 六、控制流函數 mysql有4個函數是用來進行條件操作的,這些函數可以實現sql的條件邏輯,允許開發者將一些應用程序業務邏輯轉換到數據庫后台。 mysql控制流函數: case when[test1] then [result1]...else [default] end如果testn是真,則返回resultn,否則返回default case [test] when[val1] then [result]...else [default]end 如果test和valn相等,則返回resultn,否則返回default if(test,t,f) 如果test是真,返回t;否則返回f ifnull(arg1,arg2) 如果arg1不是空,返回arg1,否則返回arg2 nullif(arg1,arg2) 如果arg1=arg2返回null;否則返回arg1 這些函數的第一個是ifnull(),它有兩個參數,並且對第一個參數進行判斷。如果第一個參數不是null,函數就會向調用者返回第一個參數;如果是null,將返回第二個參數。 如:select ifnull(1,2), ifnull(null,10),ifnull(4*null,'false'); nullif()函數將會檢驗提供的兩個參數是否相等,如果相等,則返回null,如果不相等,就返回第一個參數。 如:select nullif(1,1),nullif('a','b'),nullif(2+3,4+1); 和許多腳本語言提供的if()函數一樣,mysql的if()函數也可以建立一個簡單的條件測試,這個函數有三個參數,第一個是要被判斷的表達式,如果表達式為真,if()將會返回第二個參數,如果為假,if()將會返回第三個參數。 如:selectif(1<10,2,3),if(56>100,'true','false'); if()函數在只有兩種可能結果時才適合使用。然而,在現實世界中,我們可能發現在條件測試中會需要多個分支。在這種情況下,mysql提供了case函數,它和php及perl語言的switch-case條件例程一樣。 case函數的格式有些復雜,通常如下所示: case [expression to be evaluated] when [val 1] then [result 1] when [val 2] then [result 2] when [val 3] then [result 3] ...... when [val n] then [result n] else [default result] end 這里,第一個參數是要被判斷的值或表達式,接下來的是一系列的when-then塊,每一塊的第一個參數指定要比較的值,如果為真,就返回結果。所有的when-then塊將以else塊結束,當end結束了所有外部的case塊時,如果前面的每一個塊都不匹配就會返回else塊指定的默認結果。如果沒有指定else塊,而且所有的when-then比較都不是真,mysql將會返回null。 case函數還有另外一種句法,有時使用起來非常方便,如下: case when [conditional test 1] then [result 1] when [conditional test 2] then [result 2] else [default result] end 這種條件下,返回的結果取決於相應的條件測試是否為真。 示例: mysql>select case 'green' when 'red' then 'stop' when 'green' then 'go' end; select case 9 when 1 then 'a' when 2 then 'b' else 'n/a' end; select case when (2+2)=4 then 'ok' when(2+2)<>4 then 'not ok' end asstatus; select name,if((isactive = 1),'已激活','未激活') as result fromuserlogininfo; select fname,lname,(math+sci+lit) as total, case when (math+sci+lit) < 50 then 'd' when (math+sci+lit) between 50 and 150 then 'c' when (math+sci+lit) between 151 and 250 then 'b' else 'a' end as grade from marks; select if(encrypt('sue','ts')=upass,'allow','deny') as loginresultfrom users where uname = 'sue';#一個登陸驗證 七、格式化函數 date_format(date,fmt) 依照字符串fmt格式化日期date值 format(x,y) 把x格式化為以逗號隔開的數字序列,y是結果的小數位數 inet_aton(ip) 返回ip地址的數字表示 inet_ntoa(num) 返回數字所代表的ip地址 time_format(time,fmt) 依照字符串fmt格式化時間time值 其中最簡單的是format()函數,它可以把大的數值格式化為以逗號間隔的易讀的序列。 示例: select format(34234.34323432,3); select date_format(now(),'%w,%d %m %y %r'); select date_format(now(),'%y-%m-%d'); select date_format(19990330,'%y-%m-%d'); select date_format(now(),'%h:%i %p'); select inet_aton('10.122.89.47'); select inet_ntoa(175790383); 八、類型轉化函數 為了進行數據類型轉化,mysql提供了cast()函數,它可以把一個值轉化為指定的數據類型。類型有:binary,char,date,time,datetime,signed,unsigned 示例: select cast(now() as signed integer),curdate()+0; select 'f'=binary 'f','f'=cast('f' as binary); 九、系統信息函數 database() 返回當前數據庫名 benchmark(count,expr) 將表達式expr重復運行count次 connection_id() 返回當前客戶的連接id found_rows() 返回最后一個select查詢進行檢索的總行數 user()或system_user() 返回當前登陸用戶名 version() 返回mysql服務器的版本 示例: select database(),version(),user(); selectbenchmark(9999999,log(rand()*pi()));#該例中,mysql計算log(rand()*pi())表達式9999999次。
舉例: