您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

MySQL常见函数_MySQL

2024/4/20 16:32:48发布4次查看
bitscn.com
字符串函数concat(str1,str2,...) 返回结果为连接参数产生的字符串。如有任何一个参数为null ,则返回值为 null。 [sql] view plaincopy
mysql>%20select%20concat('my',%20's',%20'ql');
->%20'mysql'
mysql>%20select%20concat('my',%20null,%20'ql');
->%20null
mysql>%20select%20concat(14.3);
->%20'14.3'
group_concat函数%20将取得的值用逗号连接。%20[sql]%20view%20plaincopy
select%20group_concat(id)%20from%20table_name;%20得到的结果是(1,2,3,4,5)
left,right函数%20left(str,n)或者right(str,n)%20返回字符串最左边/右边的n个字符。
length函数,char_length函数%20length(str)%20char_length(str)%20length:%20是计算字段的长度一个汉字是算两个字符,一个数字或字母算一个字符%20char_length:不管汉字还是数字或者是字母都算是一个字符。
substring()%20substring(str,pos,len)%20substring(str%20from%20pos%20for%20len)%20substring(str,pos)%20substring(str%20from%20pos)%20[sql]%20view%20plaincopy
mysql>%20select%20substring(‘quadratically’,5);->%20‘ratically’
mysql>%20select%20substring(‘foobarbar’%20from%204);->%20‘barbar’
mysql>%20select%20substring(‘quadratically’,5,6);->%20‘ratica’
mysql>%20select%20substring(‘sakila’,%20-3);->%20‘ila’
mysql>%20select%20substring(‘sakila’,%20-5,%203);->%20‘aki’
mysql>%20select%20substring(‘sakila’%20from%20-4%20for%202);->%20‘ki’
substring_index(str,delim,count)%20返回字符串%20str%20中在第%20count%20个出现的分隔符%20delim%20之前的子串。%20如果%20count%20是一个正数,返回从最后的(从左边开始计数)分隔符到左边所有字符。%20如果%20count%20是负数,返回从最后的(从右边开始计数)分隔符到右边所有字符。%20mysql>select%20substring_index('www.baidu.com',%20'.',%202);%20->%20'www.baidu'%20mysql>%20select%20substring_index('www.baidu.com',%20'.',%20-2);%20->%20'baidu.com'
控制流函数case%20value%20when%20[compare-value]%20then%20result%20[when%20[compare-value]%20then%20result%20...]%20[else%20result]%20end%20case%20when%20[condition]%20then%20result%20[when%20[condition]%20then%20result%20...]%20[else%20result]%20end%20在第一个方案的返回结果中,%20value=compare-value。而第二个方案的返回结果是第一种情况的真实结果。如果没有匹配的结果值,则返回结果为else后的结果,如果没有else%20部分,则返回值为%20null。
if(expr1,expr2,expr3)函数%20如果expr1为true,则返回expr2,否则返回expr3。%20expr1%20作为一个整数值进行计算,就是说,假如你正在验证浮点值或字符串值,%20那么应该使用比较运算进行检验。%20[sql]%20view%20plaincopy
mysql>%20select%20if(1>2,2,3);
->%203
mysql>%20select%20if(1->%20'yes'
mysql>%20select%20if(strcmp('test','test1'),'no','yes');
->%20'no'
ifnull(expr1,expr2)%20假如expr1%20不为%20null,则%20ifnull()%20的返回值为%20expr1;%20否则其返回值为%20expr2。ifnull()的返回值是数字或是字符串,具体情况取决于其所使用的语境。这个函数一般用来替换null值,因为null值是不能参与数值运算的,下面这个语句就可以把null值用0来替换。%20[sql]%20view%20plaincopy
mysql>%20select%20ifnull(1,0);
->%201
mysql>%20select%20ifnull(null,10);
->%2010
mysql>%20select%20ifnull(1/0,10);
->%2010
mysql>%20select%20ifnull(1/0,'yes');
->%20'yes'
nullif(expr1,expr2)%20如果expr1%20=%20expr2%20成立,那么返回值为null,否则返回值为%20expr1。%20这和case%20when%20expr1%20=%20expr2%20then%20null%20else%20expr1%20end相同。%20[sql]%20view%20plaincopy
mysql>%20select%20nullif(1,1);
->%20null
mysql>%20select%20nullif(1,2);
->%201
coalesce函数,返回参数中第一个不为空的值%20[sql]%20view%20plaincopy
select%20coalesce(a,b,c)%20from%20table_name;%20如果a不为null,则选择a;如果a为null%20,则选择b;如果b为null,则选择c;如果a、b、c都为null,则返回null。
greatest(value1,value2,...)%20当有2或多个参数时,返回值为最大(最大值的)参数。比较参数所依据的规律同least()相同。%20[sql]%20view%20plaincopy
mysql>%20select%20greatest(2,0);
->%202
mysql>%20select%20greatest(34.0,3.0,5.0,767.0);
->%20767.0
mysql>%20select%20greatest('b','a','c');
->%20'c'时间函数curdate()%20返回当前日期,只包含年月日
unix_timestamp(),%20unix_timestamp(date)%20若无参数调用,则返回一个unix%20timestamp%20('1970-01-01%2000:00:00'%20gmt%20之后的秒数)%20作为无符号整数。若用date%20来调用unix_timestamp(),它会将参数值以'1970-01-01%2000:00:00'%20gmt后的秒数的形式返回。date%20可以是一个date%20字符串、一个%20datetime字符串、一个%20timestamp或一个当地时间的yymmdd%20或yyymmdd格式的数字。%20[sql]%20view%20plaincopy
mysql>%20select%20unix_timestamp();
->%20882226357
mysql>%20select%20unix_timestamp('1997-10-04%2022:23:00');
->%20875996580
from_unixtime()%20返回unix时间戳的日期值。
to_days(date)%20给定一个日期date,%20返回一个天数%20(从年份0开始的天数%20)。%20[sql]%20view%20plaincopy
mysql> select to_days(950501);
-> 728779
mysql> select to_days('1997-10-07');
-> 729669
datediff函数 datediff(date1,date2)用来计算两个日期之间相差的天数。
extract() 函数用于返回日期/时间的单独部分,比如年、月、日、小时、分钟等等。
round(x)返回离x最近的整数,也就是对x进行四舍五入处理 round(x,y)返回x保留到小数点后y位的值,在截取时进行四舍五入处理 truncate(x,y)返回x保留到小数点后y位的值,不进行四舍五入操作
符号函数sign(x),返回x的符号,正数为1,负数为-1,0为0
ceil(x)和ceiling(x)返回大于或等于x的最小整数 floor(x)返回小于或等于x的最大整数
md5(str),返回字符串str的md5值。常用于对应用中的数据进行加密。 select md5('123456')
inet_aton(ip地址),返回ip地址的网络字节序表示 inet_ntoa(num),返回网络字节序代码的ip地址。
bitscn.com
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录