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()
-- 其他常用函數
md5();
default();

 

發佈了70 篇原創文章 · 獲贊 7 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章