四、内置函数:

包括了字符串函数、数值函数、日期函数、流程控制函数、其他函数(获取数据库信息)...

一、字符串函数【比较常用,需要掌握】
1、 concat(s1,s2,...,sn)   #把传入的参数连接成一个字符串
select concat('abc','def');
select concat(name,' age is ',age) from users;

2、insert(str,m,n,inser_str) #将str的从m位置开始的n个字符替换为inser_str
select insert('abcdef',2,3,'123456');
select insert(name,3,2,'HAHA') from users;
select insert(name,2,2,'00') from users;

3、lower(str)/upper(str) #将字符串str转换成小写/大写
select lower('HELLO'),upper('hello');
select lower('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 则任何东西不返回
select left('123',3),right('123456',3),left('123',NULL);

5、lpad(str,n,pad)/rpad(str,n,pad) #用字符串pad对str的最左边/最右边进行填充,知道满足str含有n个字符为止
select name,lpad(name,10,'#'),rpad(name,10,'@') from users;

6、trim(str)/ltrim(str)/rtrim(str) #去除字符串str左右空格/左空格/右空格
select concat('#',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)
select strcmp('aa','bb'),strcmp('aa','aa'),strcmp('bb','aa');

9、substring(str,n,m) #返回字符串str中从n起,m个字符长度的字符串
select substring('abcdef',2,3);
select name,substring(name,1,2) as subname from users;

二、数值函数
1、abs(x) #返回x的绝对值
select abs(10),abs(-10);
select abs(age) from users;

2、ceil(x) #返回大于x的最小整数
3、floor(x) #返回小于x的最大整数
select ceil(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作用相同
select mod(null,11);

5、rand() #返回0~1之间的随机数
select rand();
select ceil(rand() * 100); #取0~100之间的整数随机数
select floor(rand() * 100);

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

7、truncate(n,m) #返回数字n被截断为m位小数的数值
select truncate(1.234,2);
select truncate(1.235,2),round(1.235,2);

三、日期函数
1、curdate() #返回当前日期
2、curtime() #返回当前时间
select curdate(),curtime();

3、now() #返回当前日期+时间
select now();

4、unix_timestamp(now())#返回unix当前时间的时间戳
select unix_timestamp(now()); #从计算机元年(1971-1-100:00:00)到现在的秒数

5、from_unixtime() #将时间戳(整数)转换为“日期+时间(xx-xx-xxxx:xx:xx)”的形式
select from_unixtime(1392853616);

6、week(now()) #返回当前时间是第几周
7、year(now()) #返回当前是XX年
8、hour(now())/hour(curtime()) #返回当前时间的小时数
9、minute(curtime()) #返回当期的分钟数
...
select week(now()),year(now()),hour(now());
select week(from_unixtime(1392853616)); #返回unix时间戳中的周期数

10、month name(now())/monthname(curdate()) #返回当前月的英文名

11、date_format(now(),"%Y-%M-%D%H:%I%S") #将当期时间格式化
select date_format(now(),"%Y-%m-%d %H:%i%s");
select date_format(now(),"%y%m%d %H:%i%s");
四、流程控制函数
1、if(value,true,false) #如果value值为真,则返回true,否则,返回false
select if (salary > 3000,'Hight','Low') from salary;
select id,salary, if (salary <=> NULL,'NULL','NOT NULL') from salary;

2、ifnull(value1,value2)#如果value1不为空,则返回value1,不然返回value2
#可以用来进行空值替换
select ifnull(salary,0.00) from salary;

3、case when [value] then … else …end #如果value值为真,执行then之后的语句,不然执行eles后的语句,不要忘记end!
select case when salary <= 3000 then "Low" else "Hight"end from salary;

五、其他函数
1、database() #当前数据库
2、version() #当前数据库版本
3、user() #当前登录用户
select database();

4、inet_aton(ip) #ip地址的网络字节顺序
select inet_aton('192.168.139.1');

5、inet_ntoa() #返回数字所代表的ip
select inet_ntoa(3232271105);

6、password(str) #返回加密的str字符串
select password("123456"); #返回一个41位长的加密字符串,只是用于给MySQL系统用户进行加密
7、md5() #在应用程序中进行数据加密,比如在C++程序中
select md5(“123456”);

http://blog.163.com/info_technology/blog/static/12781505420122755150278/

http://blog.csdn.net/zjf280441589/article/details/19547623

更多的函数:

http://www.mamicode.com/info-detail-250393.html

mySQL学习入门教程——4.内置函数的更多相关文章

  1. MySQL学习笔记_7_MySQL常用内置函数

    MySQL常用内置函数 说明: 1)可以用在SELECT/UPDATE/DELETE中,及where,orderby,having中 2)在函数里将字段名作为参数,变量的值就是字段所对应的每一行的值. ...

  2. 《zw版·Halcon入门教程与内置demo》

    <zw版·Halcon入门教程与内置demo> halcon系统的中文教程很不好找,而且大部分是v10以前的版本. 例如,QQ群: 247994767(Delphi与halcon), 共享 ...

  3. Mysql一个非常有用的内置函数今天碰到要把MySQL数据库中的varchar转换成date类型进

    Mysql一个非常有用的内置函数 今天碰到要把MySQL数据库中的varchar转换成date类型进行时间的比较和查询.在网上找了找,发现MySQL也跟其他数据库一样有自己内置的转换函数:str_to ...

  4. Python入门之 Python内置函数

    Python入门之 Python内置函数 函数就是以功能为导向,一个函数封装一个功能,那么Python将一些常用的功能(比如len)给我们封装成了一个一个的函数,供我们使用,他们不仅效率高(底层都是用 ...

  5. python学习之路-4 内置函数和装饰器

    本篇涉及内容 内置函数 装饰器 内置函数 callable()   判断对象是否可以被调用,返回一个布尔值 1 2 3 4 5 6 7 8 9 10 11 num = 10 print(callabl ...

  6. python学习 day013打卡 内置函数

    本节主要内容: 内置函数: 内置函数就是python给你提供的.拿来直接用的函数,比如print,input等等.截止到python版本3.6.2 python一共提供了68个内置函数.他们就是pyt ...

  7. Python学习:6.python内置函数

    Python内置函数 python内置函数,是随着python解释器运行而创建的函数,不需要重新定义,可以直接调用,那python的内置函数有哪些呢,接下来我们就了解一下python的内置函数,这些内 ...

  8. hive学习笔记之七:内置函数

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  9. SQL入门(2): Oracle内置函数-字符/数值/日期/转换/NVL/分析函数与窗口函数/case_decode

    本文介绍Oracle 的内置函数. 常用!  一. 字符函数 ASCII 码与字符的转化函数 chr(n)   例如 select chr(65) || chr(66) || chr(67) , ch ...

随机推荐

  1. socket - Linux 套接字

    总览 #include <sys/socket.h> mysocket = socket(int socket_family, int socket_type, int protocol) ...

  2. POJ 3237 树链剖分

    题目链接:http://poj.org/problem?id=3237 题意:给定一棵n个结点n-1条边的树. 每条边都是一个边权. 现在有4种操作 1:CHANGE I V:把(输入的)第i条边的边 ...

  3. [IOI1998]Polygon(区间dp)

    [IOI1998]Polygon 题意翻译 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘积)标记. 第一步,删除其中一条 ...

  4. Sass:RGB颜色函数-Red()、Green()、Blue()函数

    Red() 函数 red() 函数非常简单,其主要用来获取一个颜色当中的红色值.假设有一个 #f36 的颜色,如果你想得到 #f36 中的 red 值是多少,这个时候使用 red() 函数就能很简单获 ...

  5. Sass函数:数学函数-abs函数

    abs( ) 函数会返回一个数的绝对值. >> abs(10) 10 >> abs(-10) 10 >> abs(-10px) 10px >> abs( ...

  6. Linux --忘记root密码/su: Authentication failure

    如果忘记了root用户的密码,或者su root的时候,提示:su: Authentication failure 那么,可以通过以下的方式来重新设置密码,而后,再尝试,那么就可以顺利su root了 ...

  7. linux中设置虚拟域名

    一.打开tomcat安装目录下conf/server.xml这个文件在server.xml文档中找到 </Engine></Service> 接着添加上面添加以下内容(暂时先说 ...

  8. new和malloc申请内存失败后的处理

    1.c++ 标准 new 失败是抛出异常的,Visual C++ 6.0中返回一个NULL指针. 使用new(std::nothrow)可以保证失败时返回NULL; 因此完全可以 #define ne ...

  9. bzoj4987 Tree 树上背包

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4987 题解 一道还不错的题咯. 很容易发现一个结论:这 \(k\) 个点构成的一定是一个连通块 ...

  10. hihocoder 1014: Trie树(Trie树模板题)

    题目链接 #include<bits/stdc++.h> using namespace std; ; struct T { int num; T* next[]; T() { num=; ...