Chinaunix首页 | 论坛 | 博客
  • 博客访问: 386292
  • 博文数量: 112
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 800
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-29 13:41
文章分类

全部博文(112)

文章存档

2020年(1)

2018年(10)

2017年(27)

2016年(18)

2015年(31)

2014年(25)

分类: Mysql/postgreSQL

2014-11-07 18:57:13

1、字符串函数:
                     concat(s1,...sn),连接s1,...sn为一个字符串;任何字符串与NULL 进行连接的结果都将是null。
                     select concat('aa','bb','cc.dd'); #aabbcc.dd;
                     select concat('123','@#$$','db3','he()'),concat('sdfh',null); #123@#$$db3he(),null; 
                     insert(str,x,y,instr),将字符串str从第x位置开始,y个字符长的子串替换为字符串instr;
                     select insert('hello',2,3,'app'); #happo;
                    select insert('hello world!',6,1,','); #hello,world!;
                    select insert('hello',2,3,null); #null;

                     lower(str),将字符串str中所有字符变为小写;
                     upper(str),将字符串str中所有字符变为大写;
                    select lower('HELLO'),upper('world'); #hello,WORLD;
                     select lower('NULL'),upper('null'),lower(null),upper(null); #null,NULL,空,空;

                     left(str,x),返回字符串str最左边的x个字符;
                     right(str,x),返回字符串str最右边的x个字符;
                    select left('helloworld',5),right('helloworld',5); #hello,world;
                    select left('hello',1),right('hello',1); #h,o;
                    select left(null,2),right(null,3),left('null',1); #空,空,n;

                     lpad(str,n,pad),用字符串pad对str最左边进行填充,直到长度为n个字符长度;
                     rpad(str,n,pad),用字符串pad对str最右边进行填充,直到长度为n个字符长度;
                    select lpad('world',10,'hello'),rpad('hello',10,'world'); #helloworld,helloworld;
                    select lpad('null',6,'1234'); #12null;
                    select rpad(null,6,'123'),lpad(null,5,'321'); #空,空;
                    select lpad('1',5,'b'),rpad('b',5,'1'); #bbbb1,b1111;

                     ltrim(str),去掉字符串str左侧的空格;
                     rtrim(str),去掉字符串str右侧的空格;
                    select ltrim('  ab'),rtrim('12  '); #ab,12;
                    select ltrim('  |ab'),rtrim('12|  '); #|ab,12|;

                     repeat(str,x),返回str重复x次的结果;
                    select repeat('1.',3); #1.1.1.;
                    select repeat('a|',2); #a|a|;

                     replace(str,a,b),用字符串b替换字符串str中所有出现的字符串a;
                    select replace('hello','l','p'); #heppo;
                    select replace('20141107','1','一'); #20一4一一07;

                     strcmp(s1,s2),比较字符串s1和s2;比较字符串s1 和s2 的ASCII 码值的大小,s1比s2小,返回-1,s1与s2相等,返回0,s1比s2大,返回1。
                    select strcmp('ab','12'),strcmp('ab','Ab'),strcmp('a1','1a'); #返回值1,0,1;
                    select strcmp('ab','aB'),strcmp('ab','Ab'),strcmp('ab','AB'),strcmp('a','b'); #0,0,0,-1;
             
       trim(str),去掉字符串行尾和行头的空格;
                    select trim('  |ab|  '),trim('  12 '); #|ab|,12;
                     substring(str,x,y),返回从字符串str x位置起y个字符长度的字串;
                     select substring('abcdef',3,3),substring('helloworld',5,3); #cde,owo;


2、数值函数:
                  abs(x),返回x的绝对值;
                  select abs(-10.2),abs(9) #10.2,9;
                  ceil(x),返回大于x的最大整数值;
                  select ceil(0.8),ceil(2),ceil(2.8),ceil(-2.2); #1,2,3,-2;
                  floor(x),返回小于x的最大整数值;
                 select floor(0.8),floor(2),floor(3.8),floor(-3.2);#0,2,3,-4;
                  mod(x,y),返回x/y的模;
                 select mod(129,5),mod(15,10),mod(1,11),mod(null,10); #4,5,1,null;
                  rand(),返回0到1内的随机数;
                 select rand(),rand(); #随机0到1内随机数;
                 select ceil(100*rand()),floor(100*rand()); #取任意指定范围(0-100内)的任意随机数;

                  round(x,y),返回参数x的四舍五入的有y位小数的值;
                 select round(1.1),round(1.1,2),round(1,2); #1,1.10,1.00;
                  select round(10.038,2),round(0.34,3),round(0.45); #10.04,0.340,0;

                  truncate(x,y),返回数字x截断为y位小数的结果;
                  select round(1.235,2),truncate(1.235,2); #1.24,1.23;

3、日期和时间函数:
                           curdate(),返回当前日期;
                          select curdate(); #2014-11-10;
                          curtime(),返回当前时间;
                          select curtime();  #10:40:40;
                          now(),返回当前日期和时间;
                          select now();  #2014-11-10 10:40:40;
                          unix_timestamp(date),返回日期date的unix时间戳;
                          select unix_timestamp(now()); #1415587510;
                          select unix_timestamp(curdate()); #1415548800;
                          select unix_timestamp('1992-05-19'); #706204800;
                          
from_unixtime,返回unix时间戳的日期值;和unix_timestamp(date)互为你操作;
                          select from_unixtime(curdate()); #1970-08-22 10:45:10;
                          select from_unixtime(curtime());  #1970-01-02 13:17:19;
                          select from_unixtime(1415587510); #2014-11-10 10:45:10 (now());
                            
select from_unixtime(1415548800);  #2014-11-10 00:00:00 (curdate());                    
                          week(date),返回日期date为一年中的第几周;
                          select week(now),week(curdate()),week('1992-05-19'); #45,45,20;
                          year(date),返回日期date的年份;
                          select year(now()),year(curdate()); #2014,2014;
                          hour(time),返回time的小时值;
                          select hour(now()),hour(curtime()); #11,11;
                          minute(time),返回time的分钟值;
                          select minute(now()),minute(curtime()); #24,24;
                          monthname(date),返回date的月份名;
                          select monthname(now()),monthname(curdate()),monthname('1987-04-21'); #november,november,april;
                          date_format(date,fmt),返回按字符串fmt格式化日期date值;
                               %S,%s   两位数字形式的秒(00,...,59);
                               %i    两位数字形式的分(00,...,59);
                               %H   两位数字形式的小时(00,...,23);
                               %h,$l  两位数字形式的小时(01,...,12);
                               %k   数字形式的小时,24小时(0,...,23);
                               %I   数字形式的小时,12小时(1,...,12);
                               %T  24小时的时间形式(hh:mm:ss);
                               %r   24小时的时间形式(hh:mm:ss AM或hh:mm:ss PM);
                               %p  AM或PM;
                               %W  一周中每一天的名称(sunday,...,saturday);
                               %a   一周中每一天名称的缩写(sun,...,sat);
                               %d   两位数字表示月中的天数(00,...,31);
                               %e   数字形式表示月中的天数(1,...,31);
                               %D   英文后缀表示月中的天数(1st,2nd,3rd,...);
                               %w   以数字形式表示周中的天数(0=sunday,...,6=saturday);
                               %j    以3位数字表示年中的天数(001,...,366);
                               %U   周(0,1,52),其中sunday为周中的第一天;
                               %u   周(0,1,52),其中monday为周中的第一天;
                               %M   月名(january,february,...,december);
                               %b   缩写的月名(jan,feb,...,dec);
                               %m  两位数字表示的月份(01,...,12);
                               %c   数字表示的月份(1,...,12);
                               %Y  4位数字表示的年份;
                               %y   2位数字表示的年份;
                               %%  直接值"%";
                               select date_format(now(),'%c/%e/%Y %H:%i:%s');  #11/10/2014 11:52:24 ;

                          date_add(date,interval expr type),返回一个日期或时间值加上一个时间间隔的时间值;返回与所给日期date 相差interval 时间段的日期。#其中interval是间隔类型关键字,expr是一个表达式,表达式后面对应类型,type是间隔类型;
                                   hour,小时,hh;
                                   minute,分,mm;
                                   second,秒,ss;
                                   year,年,YY;
                                   month,月,MM;
                                   day,日,DD;
                                   year_month,年和月,YY-MM;
                                   day_hour,日和小时,DD hh;
                                   day_minute,日和分,DD hh:mm;
                                   day_second,日和秒,DD hh:mm:ss;
                                   hour_minute,小时和分,hh:mm;
                                   hour_second,小时和秒,hh:ss;
                                   minute_second,分钟和秒,mm:ss; 
                                  例:select now() current,date_add(now(),interval 1 month) 'a month later'; 
  #一个月后;
                                      select now() current,date_add(now(),interval '1_1' year_month) 'after five year and 1 month'; #一年一月后;

                                       也可以用负数让它返回之前的某个日期时间;
                                    例:select now() current,date_add(now(),interval '-1_-1' year_month) before_1year_1month; #一年一月之前;

                          datediff(expr,expr2),返回起始时间expr和结束时间expr2之间的天数;
                                    例:select datediff(now(),date_add(now(),interval -2 year)); #当前时间与两年前相差的天数;
                                         select datediff(now(),'1949-10-01'); #当前时间与1949年10月1日相差的天数;


4、流程函数:
                 if(value,t,f)   如果value为真,返回t;否者返回f;
                  select if(sal>10000,'high','low') from pro_file;  #如果表中字段sal大于10000,返回high,否则返回low;
                 ifnull(value1,value2)   如果value1不为空返回value1,否者返回value2;
                   select ifnull(salary,0) from salary; #如果salary不为空,返回salary的值,否则返回0;
                 case when [value1] then [result1]...else [default] end   如果value1为真,返回result1,否则返回default;
                    select case when sal>=5000 then 'high' else 'low' end result from pro_file; #
                 case [expr] when [value1] then [result1]...else [default] end   如果expr等于value1,返回result1,否则返回default;
                    select case sal when 5000 then 'low' when 7000 then 'mid' else 'high' end from pro_file; #
                     select case salary when 1000 then 'low' when 2000 then 'mid' else 'high' end from salary;#
                    select case salary when 1000 then '1st' when 2000 then '2nd' when 3000 then '3rd' when 4000 then '4th' else '5th' end from salary; #



5、其它常用函数:
                        database()   返回当前数据库名;
                         select database();
                         version()    返回当前数据库版本;
                          select version(); 
                         user()    返回当前登录用户名;
                          select user();
                         inet_aton(ip)   返回ip地址的数字表示;
                          select inet_aton('192.168.1.1'); #返回ip地址的网络字节序表示;
   一般查询ip方法:select * from salary where ip>='192.168.1.1' and ip<='192.168.1.254'; #空集;
                          select * from salary where inet_aton(ip)>=inet_aton('192.168.1.1') and inet_aton(ip)<=inet_aton('192.168.1.254'); #

                         inet_ntoa(num)   返回数字代表的ip地址;
                           select inet_ntoa(3232235777); #与inet_aton(ip)相反;
                         password(str)    返回字符串str的加密版本;返回字符串str的加密版本,一个41位长的字符串;
                           select password('123456');                           
                         md5()    返回字符串str的md5值;
                            select md5('123456');




                                   
               
                 
阅读(911) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~