Mysql研究之MySQL常用內建函數完全解析,mysql內建函數解析

來源:互聯網
上載者:User

Mysql研究之MySQL常用內建函數完全解析,mysql內建函數解析


說明:

1)可以用在SELECT/UPDATE/DELETE中,及where,orderby,having中

2)在函數裡將欄位名作為參數,變數的值就是欄位所對應的每一行的值。

3)在程式設計語言如C++中提供的函數,MySQL大部分也提供了,關於MySQL函數的完整資訊,請參閱《MySQL參考手冊》

一、字串函數【比較常用,需要掌握】

1、 concat(s1,s2,…,sn) #把傳入的參數串連成一個字串

selectconcat(‘abc’,’def’);

selectconcat(name,’ age is ‘,age) from users;

2、insert(str,m,n,inser_str) #將str的從m位置開始的n個字元替換為inser_str

selectinsert(‘abcdef’,2,3,’123456′);

selectinsert(name,3,2,’HAHA’) from users;

selectinsert(name,2,2,’00′) from users;

3、lower(str)/upper(str) #將字串str轉換成小寫/大寫

selectlower(‘HELLO’),upper(‘hello’);

selectlower(‘HELLO’) as ‘HELLO’,upper(‘hello’)as ‘HELLO’;

select* from users where upper(name) = ‘AAA’;

4、left(str,n)/right(str,n) #分別返回str最左邊/最右邊的n個字元,如果n<=> NULL 則任何東西不返回

selectleft(’123′,3),right(’123456′,3),left(’123′,NULL);

5、lpad(str,n,pad)/rpad(str,n,pad) #用字串pad對str的最左邊/最右邊進行填充,知道滿足str含有n個字元為止

selectname,lpad(name,10,’#’),rpad(name,10,’@’) from users;

6、trim(str)/ltrim(str)/rtrim(str) #去除字串str左右空格/左空格/右空格

selectconcat(‘#’,trim(” abc “),’#’),concat(‘#’,ltrim(‘ abc ‘),’#’),concat(‘#’,rtrim(‘ abc ‘),’#’);

7、replace(str,sear_str,sub_str) #將字串str中所有出現的sear_str字串替換為sub_str

select replace(‘abcdefgabcd’,’cd’,’XXX’) ;

8、strcmp(str1,str2) #以ASCII碼比較字串str1,str2,返回-1(str1< str2)/0(str1= str2)/1(str1 > str2)

selectstrcmp(‘aa’,’bb’),strcmp(‘aa’,’aa’),strcmp(‘bb’,’aa’);

9、substring(str,n,m) #返回字串str中從n起,m個字元長度的字串

selectsubstring(‘abcdef’,2,3);

selectname,substring(name,1,2) as subname from users;

二、數值函數

1、abs(x) #返回x的絕對值

selectabs(10),abs(-10);

selectabs(age) from users;

2、ceil(x) #返回大於x的最小整數

3、floor(x) #返回小於x的最大整數

selectceil(2.1),ceil(2.5),ceil(2.9),floor(2.1),floor(2.5),floor(2.9)

4、mod(x,y) #返回x/y的模,與x%y作用相同

selectmod(null,11);

遊戲編程網www.cgzhw.com有詳細的說明,這裡就不再重複了。

三、日期函數

1、curdate() #返回當前日期

2、curtime() #返回目前時間

selectcurdate(),curtime();

3、now() #返回當前日期+時間

selectnow();

4、unix_timestamp(now())#返回unix目前時間的時間戳記

selectunix_timestamp(now()); #從電腦元年(1971-1-100:00:00)到現在的秒數

5、from_unixtime() #將時間戳記(整數)轉換為“日期+時間(xx-xx-xxxx:xx:xx)”的形式

selectfrom_unixtime(1392853616);

6、week(now()) #返回目前時間是第幾周

7、year(now()) #返回當前是XX年

8、hour(now())/hour(curtime()) #返回目前時間的小時數

9、minute(curtime()) #返回當期的分鐘數

selectweek(now()),year(now()),hour(now());

selectweek(from_unixtime(1392853616)); #返回unix時間戳記中的周期數

10、monthname(now())/monthname(curdate()) #返回當前月的英文名

11、date_format(now(),”%Y-%M-%D%H:%I%S”) #將當期時間格式化

selectdate_format(now(),”%Y-%m-%d %H:%i%s”);

selectdate_format(now(),”%y%m%d %H:%i%s”);

四、流程式控制制函數

1、if(value,true,false) #如果value值為真,則返回true,否則,返回false

selectif (salary > 3000,’Hight’,’Low’) from salary;

selectid,salary, if (salary <=> NULL,’NULL’,’NOT NULL’) from salary;

2、ifnull(value1,value2)#如果value1不為空白,則返回value1,不然返回value2

#可以用來進行空值替換

selectifnull(salary,0.00) from salary;

3、casewhen [value] then … else …end #如果value值為真,執行then之後的語句,不然執行eles後的語句,不要忘記end!

selectcase when salary <= 3000 then “Low” else “Hight”end from salary;

五、其他函數

1、database() #當前資料庫

2、version() #當前資料庫版本

3、user() #當前登入使用者

selectdatabase();

4、inet_aton(ip) #ip地址的網路位元組順序

selectinet_aton(’192.168.139.1′);

5、inet_ntoa #返回數字所代表的ip

selectinet_ntoa(3232271105);

6、password(str) #返回加密的str字串

selectpassword(“123456″); #返回一個41位長的加密字串,只是用於給MySQL系統使用者進行加密

7、md5() #在應用程式中進行資料加密,比如在C++程式中

selectmd5(“123456”);


mysql中函數replace的用法?

set simtime=replace(time,'-','');即可

mysql的內建函數都是全域函數,沒有對象的概念 ,所以不能使用set simtime=time.replace('-','')類似的文法。
 
麻煩幫忙指點一下幾個常用的mysql的內建函式的使用,

你應該先下載個PHP中文文檔。網上很多的。
下載完後在HI我,我的百度HI一直線上的。
不要怕打擾我!
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.