MySql常用函數全部彙總

1.字符串函數
#CHAR_LENGTH(str)字符串函數

select CHAR_LENGTH(name) from employee02  where name = '王小明';

#CONCAT(s1,s2,…)返回連接參數產生的字符串,一個或多個待拼接的內容,任意一個爲NULL則返回值爲NULL

select CONCAT("-","name","data","data01","data02")  as aaa;

#CONCAT_WS(x,s1,s2,…)返回多個字符串拼接之後的字符串,每個字符串之間有一個x

select CONCAT_WS("-","name","data","data01","data02")  as aaa;
SELECT CONCAT_WS("-", "SQL", "Tutorial", "is", "fun!")AS ConcatenatedString;

#LOWER(str)和LCASE(str)、UPPER(str)和UCASE(str)前兩者將str中的字母全部轉換成小寫,後兩者將字符串中的字母全部轉換成大寫

#LEFT(s,n)、RIGHT(s,n)前者返回字符串s從最左邊開始的n個字符,後者返回字符串s從最右邊開始的n個字符

select 
left(name,1),
left(name,2),
left(name,3) 全名
from employee02  where name = '王小明';

select 
right(name,1),
right(name,2),
right(name,3) 全名
from employee02  where name = '王小明';

#LPAD(s1,len,s2)、RPAD(s1,len,s2)前者返回s1,其左邊由字符串s2填補到len字符長度,假如s1的長度大於len,則返回值被縮短至len字符;前者返回s1,其右邊由字符串s2填補到len字符長度,假如s1的長度大於len,則返回值被縮短至len字符

select 
LPAD(id,5,'0')  id
from employee02  where name = '王小明';

select 
RPAD(id,5,'0')  id
from employee02  where name = '王小明';

#REPEAT(s,n)返回一個由重複字符串s組成的字符串,字符串s的數目等於n

select 
REPEAT(1,6)  id
from employee02  where name = '王小明';

select 
REPLACE(name,'小','明')
from employee02  where name = '王小明';

#SUBSTRING(s,n,len)、MID(s,n,len)兩個函數作用相同,從字符串s中返回一個第n個字符開始、長度爲len的字符串

select 
SUBSTRING(name,1,2)  name 
from employee02  where name = '王小明';

select 
MID(name,1,2)  name 
from employee02  where name = '王小明';

#LOCATE(str1,str)、POSITION(str1 IN str)、INSTR(str,str1)三個函數作用相同,返回子字符串str1在字符串str中的開始位置(從第幾個字符開始)

select 
LOCATE('王',name)  name 
from employee02  where name = '王小明';

select 
POSITION('小' in name)  name 
from employee02  where name = '王小明';

select 
INSTR(name,'小')  name 
from employee02  where name = '王小明';

2.日期和時間函數

select 
date
from employee02  where name = '王小明';

#CURDATE()、CURRENT_DATE

select 
CURDATE()
from employee02  where name = '王小明';

select 
CURRENT_DATE
from employee02  where name = '王小明';

#CURRENT_TIMESTAMP()、LOCALTIME()、NOW()、SYSDATE()

select 
CURRENT_TIMESTAMP()
from employee02  where name = '王小明';

select 
LOCALTIME()
from employee02  where name = '王小明';

select 
NOW()
from employee02  where name = '王小明';

select 
SYSDATE()
from employee02  where name = '王小明';

#UNIX_TIMESTAMP()、UNIX_TIMESTAMP(date)

select 
UNIX_TIMESTAMP()
from employee02  where name = '王小明';

select 
UNIX_TIMESTAMP(date)
from employee02  where name = '王小明';

#MONTH(date)和MONTHNAME(date)

select 
MONTH(date)-1   當前月份,
MONTHNAME(date)  下個月
from employee02  where name = '王小明';

#DAYOFYEAR(d)、DAYOFMONTH(d)前者返回d是一年中的第幾天,後者返回d是一月中的第幾天

select 
DAYOFYEAR(2)
from employee02 where name = '王小明';

3.條件判斷函數

SELECT IF(1<2,2,3);
SELECT IF(1>2,2,3);

select 
name,
if(name = '王小明','yes','no')  判斷結果
from employee02 where name = '王小明';


select 
name,
if(name = '王二明','yes','no')  判斷結果
from employee02 where name = '王小明';

select 
name,
ifNull(name,'無')  結果
from employee02 where name = '王小明';

select 
case name when '王小明' then '王小明' else '其他' end   結果
from employee02 where name = '王小明';

select 
case name when '王二明' then '王小明' else '其他' end   結果
from employee02 where name = '王小明';

4.系統信息函數

select VERSION();

select CONNECTION_ID();

show processlist;

#USER()、CURRENT_USER()、SYSTEM_USER()、SESSION_USER()查看當前被MySQL服務器驗證的用戶名和主機的組合,一般這幾個函數的返回值是相同的

select USER();
select CURRENT_USER();
select SYSTEM_USER();
select SESSION_USER();

#CHARSET(str)查看字符串str使用的字符集

select 
CHARSET(name)
from employee02 where name = '王小明';

5.加密函數
#PASSWORD(str)從原明文密碼str計算並返回加密後的字符串密碼,注意這個函數的加密是單向的(不可逆),因此不應將它應用在個人的應用程序中而應該只在MySQL服務器的鑑定系統中使用

select 
PASSWORD(name)
from employee02 where name = '王小明';

#爲字符串算出一個MD5 128比特校驗和,改值以32位十六進制數字的二進制字符串形式返回

select 
MD5(name)
from employee02 where name = '王小明';
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章