myaql常用函数
返回集合中最小的值
返回x的自然对数
返回x/y的模(余数)
返回数字x截短为y位小数的结果
三、字符串函数
将字符串str从第x位置开始,y个字符长的子串替换为字符串instr,返回结果
ltrim(str) 从字符串str中切掉开头的空格
strcmp(s1,s2)比较字符串s1和s2
trim(str)去除字符串首部和尾部的所有空格
四、日期和时间函数
curdate()或current_date() 返回当前的日期
curtime()或current_time() 返回当前的时间
date_add(date,interval int
keyword)返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化),如:
selectdate_add(current_date,interval 6 month);
date_format(date,fmt) 依照指定的fmt格式格式化日期date值
date_sub(date,interval int
keyword)返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化),如:
selectdate_sub(current_date,interval 6 month);
dayofweek(date)
返回date所代表的一星期中的第几天(1~7)
dayofmonth(date) 返回date是一个月的第几天(1~31)
dayofyear(date)
返回date是一年的第几天(1~366)
dayname(date)
返回date的星期名,如:select dayname(current_date);
from_unixtime(ts,fmt)
根据指定的fmt格式,格式化unix时间戳
tshour(time)
返回time的小时值(0~23)
minute(time)
返回time的分钟值(0~59)
month(date)
返回date的月份值(1~12)
monthname(date)
返回date的月份名,如:select monthname(current_date);
now()
返回当前的日期和时间
quarter(date)
返回date在一年中的季度(1~4),如select quarter(current_date);
week(date)
返回日期date为一年中第几周(0~53)
year(date)
返回日期date的年份(1000~9999)
一些示例:
获取当前系统时间:select from_unixtime(unix_timestamp());
select extract(year_month from current_date);
select extract(day_second from current_date);
select extract(hour_minute from current_date);
返回两个日期值之间的差值(月数):select period_diff(200302,199802);
在mysql中计算年龄:
select
date_format(from_days(to_days(now())-to_days(birthday)),'%y')+0 as
age from employee;
这样,如果brithday是未来的年月日的话,计算结果为0。
下面的sql语句计算员工的绝对年龄,即当birthday是未来的日期时,将得到负值。
select date_format(now(), '%y') - date_format(birthday, '%y')
-(date_format(now(), '00-%m-%d')
五、加密函数
aes_encrypt(str,key)
返回用密钥key对字符串str利用高级加密标准算法加密后的结果,调用aes_encrypt的结果是
一个二进制字符串,以blob类型存储
aes_decrypt(str,key)
返回用密钥key对字符串str利用高级加密标准算法解密后的结果
decode(str,key)
使用key作为密钥解密加密字符串
strencrypt(str,salt)
使用unixcrypt()函数,用关键词salt(一个可以惟一确定口令的字符串,就像钥匙一样)加密字符串
strencode(str,key)
使用key作为密钥加密字符串str,调用encode()的结果是一个二进制字符串,它以blob类型存储
md5()
计算字符串str的md5校验
password(str)
返回字符串str的加密版本,这个加密过程是不可逆转的,和unix密码加密过程使用不同的算法。
sha()
计算字符串str的安全散列算法
(sha)校验和示例:
select encrypt('root','salt');
select encode('xufeng','key');
select decode(encode('xufeng','key'),'key');#加解密放在一起
select aes_encrypt('root','key');
select aes_decrypt(aes_encrypt('root','key'),'key');
select md5('123456');
select sha('123456');
mysql有4个函数是用来进行条件操作的,这些函数可以实现sql的条件逻辑,允许开发者将一些应用程序业务逻辑转换到数据库后台。
end如果testn是真,则返回resultn,否则返回default
[default]end 如果test和valn相等,则返回resultn,否则返回
default
如果test是真,返回t;否则返回f
ifnull(null,10),ifnull(4*null,'false');
和许多脚本语言提供的if()函数一样,mysql的if()函
case函数的格式有些复杂,通常如下所示:
case [e x p r e s s i o n to be e v a l u a t e
d]
when [val 1] then [result 1]
when [val 2] then [result 2]
when [val 3] then [result 3]
......
when [val n] then [result n]
else [default result]
end
这里,第一个参数是要被判断的值或表达式,接下来的是一系列的when-then块,每一块的第一个参数指定要比较的值,如果为真,就返回结果。所有的when-then块将以else块结束,当end结束了所有外部的case块时,如果前面的每一个块都不匹配就会返回else块指定的默认结果。如果没有指定else块,而且所有的when-then比较都不是真,mysql将会返回null。
case函数还有另外一种句法,有时使用起来非常方便,如下:
case when [conditional test 1] then [result 1]
when [conditional test 2] then [result 2]
else [default result]
end
这种条件下,返回的结果取决于相应的条件测试是否为真。
示例:
mysql>select case 'green'
when 'red' then 'stop'
when 'green' then 'go' end;
select case 9 when 1 then 'a' when 2 then 'b' else 'n/a' end;
select case when (2+2)=4 then 'ok' when
(2+2)<>4 then 'not ok' end asstatus;
select name,if((isactive = 1),'已激活','未激活') as result
fromuserlogininfo;
select fname,lname,(math+sci+lit) as total,
case when (math+sci+lit) < 50 then 'd'
when (math+sci+lit) between 50 and 150 then
'c'
when (math+sci+lit) between 151 and 250 then 'b'
else 'a' end as grade from marks;
select if(encrypt('sue','ts')=upass,'allow','deny') as
loginresultfrom users where uname = 'sue'; 一个登陆验证
七、格式化函数
date_format(date,fmt) 依照字符串fmt格式化日期date值
format(x,y)
把x格式化为以逗号隔开的数字序列,y是结果的小数位数
inet_aton(ip)
返回ip地址的数字表示
inet_ntoa(num) 返回数字所代表的ip地址
time_format(time,fmt) 依照字符串fmt格式化时间time值
其中最简单的是
format()函数,它可以把大的数值格式化为以逗号间隔的易读的序列。
示例:
select format(34234.34323432,3);
select date_format(now(),'%w,%d %m %y %r');
select date_format
(now(),'%y-%m-%d');
select date_format(19990330,'%y-%m-%d');
select date_format(now(),'%h:%i %p');
select inet_aton('10.122.89.47');
select inet_ntoa(175790383);
八、类型转化函数
为了进行数据类型转化,mysql提供了cast()函数,它可以把一个值转化为指定的数据类型。
类型有:binary,char,date,time,datetime,signed,unsigned
示例:
select cast(now() as signed integer),curdate()+0;
select 'f'=binary 'f','f'=cast('f' as binary);
九、系统信息函数
database() 返回当前数据库名
benchmark(count,expr) 将表达式expr重复运行count次
connection_id()
返回当前客户的连接
idfound_rows()
返回最后一个select查询进行检索的总行数
user()或system_user() 返回当前登陆用户名
version() 返回mysql服务器的版本
示例:
select database(),version(),user();
selectbenchmark(9999999,log(rand()*pi()));#该例中,mysql计算
log(rand()*pi())表达式9999999次。
myaql常用函数的更多相关文章
- oracle常用函数及示例
学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...
- 总结js常用函数和常用技巧(持续更新)
学习和工作的过程中总结的干货,包括常用函数.常用js技巧.常用正则表达式.git笔记等.为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. PS:此文档,我会持续更新. Aj ...
- [转]SQL 常用函数及示例
原文地址:http://www.cnblogs.com/canyangfeixue/archive/2013/07/21/3203588.html --SQL 基础-->常用函数 --===== ...
- PHP常用函数、数组方法
常用函数:rand(); 生成随机数rand(0,50); 范围随机数时间:time(); 取当前时间戳date("Y-m-d H:i:s"); Y:年 m:月份 d:天 H:当前 ...
- Oracle常用函数
前一段时间学习Oracle 时做的学习笔记,整理了一下,下面是分享的Oracle常用函数的部分笔记,以后还会分享其他部分的笔记,请大家批评指正. 1.Oracle 数据库中的to_date()函数的使 ...
- Thinkcmf:页面常用函数
Thinkcmf:页面常用函数 全站seo: 文章列表: {$site_seo_title} <!--SEO标题--> {$site_seo_keywords} < ...
- matlab进阶:常用功能的实现,常用函数的说明
常用功能的实现 获取当前脚本所在目录 current_script_dir = fileparts(mfilename('fullpath')); % 结尾不带'/' 常用函数的说明 bsxfun m ...
- iOS导航控制器常用函数与navigationBar常用属性
导航控制器常用函数触发时机 当视图控制器的View将要出现时触发 - (void)viewWillAppear:(BOOL)animated 当视图控制器的View已经出现时触发 - (void)vi ...
- 《zw版·Halcon-delphi系列原创教程》 zw版-Halcon常用函数Top100中文速查手册
<zw版·Halcon-delphi系列原创教程> zw版-Halcon常用函数Top100中文速查手册 Halcon函数库非常庞大,v11版有1900多个算子(函数). 这个Top版,对 ...
随机推荐
- iOS开发——多线程篇——快速生成沙盒目录的路径,多图片下载的原理、SDWebImage框架的简单介绍
一.快速生成沙盒目录的路径 沙盒目录的各个文件夹功能 - Documents - 需要保存由"应用程序本身"产生的文件或者数据,例如:游戏进度.涂鸦软件的绘图 - 目录中的文件会被 ...
- BZOJ1251——序列终结者
给你一个数列,让你实现区间加上一个值,区间翻转,区间最大值 裸splay,懒标记一发即可 #include <cstdio> #include <cstdlib> #inclu ...
- 通过NavMeshObstacle解决NavMesh防卡
http://www.unity蛮牛.com/thread-33383-1-1.html. 许久未曾发帖了,最近忙于换工作的问题,经常处于纠结状态,so...偶尔上蛮牛还能看到大家对我的支持,感觉还是 ...
- 下载老版本的Xcode
1.苹果开发者中心,找到Xcode 2.点击下载 3,找到Support 4.找到所需的版本,点击"+"下载 5.安装Xcode,愉快的开发.
- Delphi实现窗体内嵌其他应用程序窗体
实现原理是启动一个应用程序,通过ProcessID得到窗体句柄,然后对其设定父窗体句柄为本程序某控件句柄(本例是窗体内一个Panel的句柄),这样就达成了内嵌的效果. 本文实现的是内嵌一个记事本程序, ...
- backbone模型层浅析
Model层有两个类: Model, Collection 1.Model 不翻文档,我们用代码说话. 首先分析下类. var myM = Backbone.Model.extend({})//构造一 ...
- C# XML和实体类之间相互转换(序列化和反序列化)
我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. using System; using System.Collections.Ge ...
- Colorful tree
cnbb 我被数组清零卡了一天.. 子树改色询问子树颜色数.. 先考虑颜色为x的节点对祖先答案的贡献,那么我们考虑把所有这些节点都搞出来,按dfs序排序,然后考虑每个节点a掌管的祖先是它和按dfs序的 ...
- phpcms后台进入地址(包含No permission resources错误)
安装phpcms后却不知道怎么进入后台,实际上输入如下地址即可进入后台登陆界面: http://你的域名/admin.php 如果出现No permission resources.错误,可能是之前修 ...
- ubuntu14.04 server 安装vmware worktation 12
0) Do the basic system installation of Ubuntu 14.04 LTS (Server or Desktop) 1) wget the installer wg ...