MYSQL常用函數包含字符串函數、數值函數、日期函數和系統信息函數。
字符串函數:
1. 合併字符串函數:concat(str1,str2,str3…)
2. 比較字符串大小函數:strcmp(str1,str2)
3. 獲取字符串字節數函數:length(str)
4. 獲取字符串字符數函數:char_length(str)
5. 字母大小寫轉換函數:大寫:upper(x);小寫lower(x)
6. 字符串查找函數:find_in_set(str1,str2)
7. 獲取指定位置的子串:field(str,str1,str2...)
8. 字符串去空函數:ltrim(str)//頭部去空格;rtrim(str)//結尾去空格;trim(str)//首尾去空格
9. 字符串替換函數:insert(str,pos,len,newstr) //pos開始位置,len替換長度
10. 字符串截取函數:從左邊截取:left(str,num)
從右邊截取:right(str,num)
11. 截取指定位置和長度子字符串:substring(str,num,len) //num開始位置,len截取長度
12. 替換字符串:replace(str,a,b) //使用字符串b替換字符串a
數值函數:
l 絕對值函數:abs(x)
l 向上取整函數:ceil(x)
l 向下取整函數:floor(x)
l 取模函數:mod(x,y)
l 隨機數函數:rand() //返回0~1內的隨機數
l 四捨五入函數:round(x,y)
l 數值截取函數:truncate(x,y)
l PI() 返回圓周率(3.141593) SELECT PI() --3.141593
l POW(x,y).POWER(x,y) 返回x的y次方 SELECT POW(2,3) -- 8
l SQRT(x) 返回x的平方根 SELECT SQRT(25) -- 5
l EXP(x) 返回e的x次方 SELECT EXP(3) -- 20.085536923188
日期函數:
a. 獲取當前日期:curdate(),current_date()
b. 獲取當前時間:curtime(),current_time()
c. 獲取當前日期時間:now()
d. 從日期中選擇出月份數:month(date),monthname(date)
e. 從日期中選擇出週數:week(date)
f. 從日期中選擇出週數:year(date)
g. 從時間中選擇出小時數:hour(time)
h. 從時間中選擇出分鐘數:minute(time)
i. 從時間中選擇出今天是周幾:weekday(date),dayname(date)
系統信息函數:
l version() 返回數據庫版本號
l database() 返回當前數據庫名
l user() 返回當前用戶
l last_insert_id() 返回最近生成的AUTO_INCREMENT值
使用技巧參考:1