好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MySQL学习笔记_7_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);



5 、 rand() # 返回 0 ~ 1 之间的随机数

selectrand();

selectceil(rand() * 100); # 取 0 ~ 100 之间的整数随机数

selectfloor(rand() * 100);



6 、 round(n,m) # 返回 n 四舍五入之后含有 m 位小数的值, m 值默认为 0

selectround(1.23);

selectround(1.456,2);



7 、 truncate(n,m) # 返回数字 n 被截断为 m 位小数的数值

selecttruncate(1.234,2);

selecttruncate(1.235,2),round(1.235,2);



三、日期函数

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学习笔记_7_MySQL常用内置函数

标签:mysql   select   加密   database   md5   

查看更多关于MySQL学习笔记_7_MySQL常用内置函数的详细内容...

  阅读:24次