mysql----常用函数

        
常用函数:
    按功能分类为:字符串函数、数值函数、日期时间函数、流程函数、其他函数
        字符串函数:
            concat(s1,2,...sn):字符串拼接,任何字符串与null连接都是null
            insert(str,x,y,instr):将str的x开始的y个字符换成instr
                select insert('helloworld',2,3,'AAAAAA');  
                #hAAAAAAoworld
            left(str,n):获得str从左边开始的n个字符,如果x为null不返回任何字符
            right(str,n):获得str从右边开始的n个字符,如果x为null不返回任何字符
            lpad(str,n,pad):用pad在str的左边进行填充,直到长度是n
                #select lpad('hello',20,'pad')
                padpadpadpadpadhello
            rpad(str,n,pad):用pad在str的右边进行填充,直到长度是n
                #select rpad('hello',20,'pad')
                hellopadpadpadpadpad
            lstrim(str),rstrim(str),strim(str):去除str左边、右边、或左右两边的空格
            repeat(str,n):将str重复n次
            replace(str,a,b):用b替换str中的a
                #select replace('helloworld','o','OOO');
                #hellOOOwOOOrld
            substr(str,start,count):截取str中start开始的count个字符
                #select substr('helloworld',2,3);
                ell
                
        数值函数:
            abs(x):取x的绝对值
            ceil(x):取大于x的最小整数
            floor(x):取小于x的最大整数
            mod(x,y):x对y取模,数学上的余数
            rand():得到0-1之间的任意一个数
            
        日期时间函数:
            curdate():得到当前日期,如:2019-12-22
            curtime():得到当前时间,如:20:15:58
            now():得到当前日期时间,如:2019-12-22 20:16:20
            unix_timestamp():得到当前时间戳:1577017048
            from_unixtime(unixtime):从时间戳换成普通时间
                select FROM_UNIXTIME(UNIX_TIMESTAMP());
                2019-12-22 20:19:44
            week(date):得到这个日期是一年中的第几周
            year(date):得到这个日期的年份
            hour(time):得到当前时间的小时数
            minute(time):得到当前分钟数
            date_format(date,fmt):
                select date_format(now(),'%M,%D,%Y');
                #December,22nd,2019
            date_add(date,interval xx xx ):
                select date_add(now(),interval 1 day);
                #2019-12-23 20:28:43
            datediff(date1,date2):日期求差,若差距小于1天那么值都是0
                #select datediff(now(), date_add(now(),interval 1 week));
                #-7
        
        流程函数:
            if(value,t,f):如果value为真返回true,否则返回false;
                #select if(2>3,'true','false');
                #false
            ifnull(value1,value2):如果value1不为null则返回value1,否则返回value2
                #select ifnull(null,'true');
                true
            case while then end:
        
        其他函数:
            datebase():返回当前数据库
            version():得到当前数据库版本
            user():得到当前用户
            password(str):加密
            MD5(str):加密
    

发布了76 篇原创文章 · 获赞 21 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/shen_chengfeng/article/details/103656811