mysql函数及解析,Mysql研究之MySQL常用内置函数完全解析
說明: 1 )可以用在 SELECT/UPDATE/DELETE 中,及 where , orderby , having 中 2 )在函數里將字段名作為參數,變量的就是字段所對應的每一行的。 3 )在程序設計語言如 C 中提供的函數, MySQL 大部分也提供了,關于 MySQL 函數的完整信息,請參閱《 My
說明:
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”);
本條技術文章來源于互聯網,如果無意侵犯您的權益請點擊此處反饋版權投訴
本文系統來源:php中文網
總結
以上是生活随笔為你收集整理的mysql函数及解析,Mysql研究之MySQL常用内置函数完全解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2023新一代人工智能(深圳)创业大赛评
- 下一篇: endnote初始化数据库支持_5 个免