Mysqlz之常見函數

一、定義

類似於編程語言的方法,將一組邏輯語句封裝在方法體中,對外暴露方法名。

  • 好處:1.隱藏了實現細節 2.提高代碼重用性
  • 調用:select 函數名(實參列表)【from 表】
  • 分類:
    1.單行函數,如concat,length,ifnull等
    2.分組函數:做統計使用,又叫統計函數,聚合函數,組函數

二、字符函數

#LENGTH(str) 獲取參數值的字節數
select length('mark') //4
select length('中國') //6 utf-8 中文佔3個字符

#CONCAT(str1,str2,...) 拼接字符串
select CONCAT('aa','bb') // aabb

#UPPER(str) 大寫、LOWER(str) 小寫
select CONCAT(UPPER('aaa'),LOWER('BBB')) // AAAbbb

#SUBSTR(str FROM pos FOR len)、SUBSTRING(str FROM pos FOR len)
#索引從1開始
select SUBSTR('唐吉坷德多佛朗明哥', 5) // 截取從指定索引處到最後
select SUBSTR('唐吉坷德多佛朗明哥', 1, 4) // 截取從指定索引處,指定字符長度

#INSTR(str,substr) 返回子串第一次出現的索引,找不到返回0
select instr('劍豪世界第一大劍豪', '劍豪') // 1

#TRIM([remstr FROM] str)
select trim('   任我行    ') // 任我行
select trim('a' from 'aaaaaa任我aa行aaaaa') // 任我aa行

#LPAD(str,len,padstr) 用指定字符實現左填充長度
select lpad('張三丰', 10, 'a') // aaaaaaa張三丰

#RPAD(str,len,padstr) 用指定字符實現左填充長度
select rpad('張三丰', 10, 'a') // 張三丰aaaaaaa

#REPLACE(str,from_str,to_str)
select replace('日月神教教主任我行', '任我行', '東方不敗') // 日月神教教主東方不敗

三、數學函數

# ROUND(X) 四捨五入
select round(4.5) // 5
select round(-4.5) // -5
select round(-4.555, 2) // -4.56 小數點後保存兩位 

#CEIL(X) 向上取整
select ceil(1.01) // 2
select ceil(-1.01) // -1

#FLOOR(X) 向下取整
select floor(-1.01) // -2

#`TRUNCATE`(X,D) 截斷
select truncate(1.69, 1) // 1.6

#mod 取餘
mod(a,b) // a-a/b*b  除號兩邊是整數,結果取整
select MOD(-10, -3) // -1

四、日期函數

#NOW() 當前系統日期+時間
select now() // 2019-10-29 15:07:31

#CURDATE() 當前系統日期
select curdate() // 2019-10-29

#CURTIME() 當前時間 不含日期
select curtime() // 15:09:14

#返回指定部分,年、月、日、小時、分鐘、秒
select YEAR('2019-01-01') // 2019
select MONTH('2019-01-01') // 1

#STR_TO_DATE(str,format) 將日期格式字符轉換爲指定日期
select str_to_date('3-4-2019', '%d-%c-%Y') // 2019-04-03

#DATE_FORMAT(date,format) 將日期轉換爲字符
select date_format(now(), '%y年%m月%d日') // 19年10月29日

五、其他函數

select version()
select database()
select user()

六、流程控制函數

select if(10 > 5, '大', '小') // 大

#case函數使用一: switch case 的效果
/*
case 要判斷的字段或表達式
when 常量值1 then 要顯示的值1或語句1;
when 常量值2 then 要顯示的值2或語句2;
...
else 要顯示的值n或語句n;
end
*/

select salary 原始工資,department_id,
case department_id
when 30 then salary*1.1
when 40 then salary*1.2
when 50 then salary*1.3
else salary
end as 新工資
from employees


#case函數使用二: 多重if
/*
case 
when 條件1 then 要顯示的值1或語句1;
when 條件2 then 要顯示的值2或語句2;
...
else 要顯示的值n或語句n;
end
*/

select salary, case 
when salary > 20000 then 'A'
when salary > 15000 then 'B'
when salary > 10000 then 'C'
else 'D'
END as 工資級別
from employees

七、分組函數

# 簡單使用
select sum(salary) from employees // 工資之和
select avg(salary) from employees // 平均工資
select max(salary) from employees // 最大工資
select min(salary) from employees // 最小工資
select count(salary) from employees // salary 字段非空有幾個

# 參數類型
sum(),avg() 處理數值型
max(),min(),count() 可處理任何類型
以上分組函數都忽略null值
和分組函數一起查詢的字段要求是group by 後的字段
select count(*) from employees // 一般用這個
select count(1) from employees // 性能和上一個差不多
select count(salary) from employees //salary 爲null的數據不統計 

# 和distinct搭配
select sum(distinct salary), sum(salary) from employees
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章