1. 程式人生 > 其它 >MySql常用語句函式總結

MySql常用語句函式總結

-- 數值函式
abs(x) -- 絕對值 abs(-10.9) = 10
format(x, d) -- 格式化千分位數值 format(1234567.456, 2) = 1,234,567.46
ceil(x) -- 向上取整 ceil(10.1) = 11
floor(x) -- 向下取整 floor (10.1) = 10
round(x) -- 四捨五入去整
mod(m, n) -- m%n m mod n 求餘 10%3=1
pi() -- 獲得圓周率
pow(m, n) -- m^n
sqrt(x) -- 算術平方根
rand() -- 隨機數
truncate(x, d) -- 擷取d位小數

-- 時間日期函式
now(), current_timestamp(); -- 當前日期時間
current_date(); -- 當前日期
current_time(); -- 當前時間
date('yyyy-mm-dd hh:ii:ss'); -- 獲取日期部分
time('yyyy-mm-dd hh:ii:ss'); -- 獲取時間部分
date_format('yyyy-mm-dd hh:ii:ss', '%d %y %a %d %m %b %j'); -- 格式化時間
unix_timestamp(); -- 獲得unix時間戳
from_unixtime(); -- 從時間戳獲得時間

-- 字串函式
length(string) -- string長度,位元組
char_length(string) -- string的字元個數
substring(str, position [,length]) -- 從str的position開始,取length個字元
replace(str ,search_str ,replace_str) -- 在str中用replace_str替換search_str
instr(string ,substring) -- 返回substring首次在string中出現的位置
concat(string [,...]) -- 連線字串
charset(str) -- 返回字串字符集
lcase(string) -- 轉換成小寫
left(string, length) -- 從string2中的左邊起取length個字元
load_file(file_name) -- 從檔案讀取內容
locate(substring, string [,start_position]) -- 同instr,但可指定開始位置
lpad(string, length, pad) -- 重複用pad加在string開頭,直到字串長度為length
ltrim(string) -- 去除前端空格
repeat(string, count) -- 重複count次
rpad(string, length, pad) --在str後用pad補充,直到長度為length
rtrim(string) -- 去除後端空格
strcmp(string1 ,string2) -- 逐字元比較兩字串大小

-- 流程函式
case when [condition] then result [when [condition] then result ...] [else result] end 多分支
if(expr1,expr2,expr3) 雙分支。

-- 聚合函式
count()
sum();
max();
min();
avg();
group_concat()